MATLAB Answers

Coding inter-argument dependencies into inputParser

6 views (last 30 days)
Daniel Lyddy
Daniel Lyddy on 24 Feb 2016
There are often times when I wish I could embed inter-argument dependencies into the inputParser.
For example, I may be passing a matrix in to a function as a required argument. I may further want to allow the caller to pass in an optional argument that tells the function to operate on just a subblock of the main input matrix. There are two dependencies, then, on the size of the original matrix:
1. I want the 'subblock' option to default to the whole input matrix, and,
2. I want to check 'subblock' to ensure that it does not specify a subblock that is larger than the input matrix.
As near as I can tell, there is currently no way to accomplish this cleanly using the inputParser mechanism. To work around this shortcoming, I have to:
1. Set the default for 'subblock' to the empty argument [], and trap that empty condition after I call inputParser.parse, so I can set 'subblock' to extract the whole matrix, and,
2. If 'subblock' is provided, again, perform a test outside of inputParser to make sure none of subblock's elements are out-of-bounds.
This is clunky. Note: in the following example, subblock is encoded in [top, bottom, left, right] order.
% instantiate parser
p = inputParser;
% define validators
validInput = @(x) validateattributes(x, {'numeric'}, {'2d'});
validSub = @(x) validateattributes(x, {'numeric'}, {'integer', 'vector', 'positive', 'numel', 4});
% defaults
% add required arguments
p.addRequired('input', validInput);
% add optional parameter-value pairs
p.addParameter('subblock', DEFAULT_SUB, validSub);
% parse inputs
p.parse(input, varargin{:});
% copy optional parameters to local variables
subblock = p.Results.subblock;
% set/check subblock
inputSize = size(input);
if isempty(subblock)
% default is whole input
subblock = [1, inputSize(1), 1, inputSize(2)];
% check that we are not out of bounds
assert(all(subblock(1:2) <= inputSize(1)) && all(subblock(3:4) <= inputSize(2)), ...
error('%s: one or more subblock indices are out of bounds\n', mfilename));
Ugh. There has to be a cleaner way to accomplish this.


Sign in to comment.

Answers (0)

Sign in to answer this question.