I have a function for which I would like to pass arguments through varargin and use inputParser to ensure that the inputs are normal. Some arguments are required, and some are not. Here is an example:
function myFunc(varargin) p = inputParser; p.addRequired(... 'numStates', ... @(x) validateattributes(x, {'numeric'}, ... {'scalar', 'integer', 'positive'})); p.addRequired(... 'numInps', ... @(x) validateattributes(x, {'numeric'}, ... {'scalar', 'integer', 'positive'})); p.addRequired(... 'numOuts', ... @(x) validateattributes(x, {'numeric'}, ... {'scalar', 'integer', 'positive'})); p.addRequired(... 'X0', ... @(x) validateattributes(x, {'numeric'}, ... {'vector'})); p.addOptional(... 'freq', 10, ... @(x) validateattributes(x, {'numeric'}, ... {'scalar', 'integer', 'positive'})); p.addOptional(... 'SimulinkVariables', struct(), ... @(x) isa(x, 'struct')); p.parse(varargin{:}); %
I would like to be able to pass arguments as follows: it does not matter which pair passes, when, while there are necessary ones. Thus, an example could be as follows:
myFunc('numStates', 4, 'numInps', 2, 'numOUts', 3, 'X0', [4;0]);
This syntax seems to be illegal; parse() expects the first arguments in it to be the required values, but they should not be explicitly named, that is, as in:
function myFunc(numStates, numInps, numOuts, X0, varargin) ... p.parse(numStates, numInps, numOuts, X0, varargin{:}); end
Is there an easy way to get this to do what I want, i.e. first function? I think the easiest way is to do something to reorder the varargin elements and throw out the argument names, but this is not very elegant.