Coding inter-argument dependencies into inputParser

1 view (last 30 days)
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
DEFAULT_SUB = [];
% 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)];
else
% 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));
end
Ugh. There has to be a cleaner way to accomplish this.

Answers (0)

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!