Input parser: default argument depends on another required argument, how to do this?
Ältere Kommentare anzeigen
In an attempt to clean up my code and make it usable for the outside world, I would like to have decent input handling. Some inputs are optional, in the sense that they are not needed, but will be used if available. It seems reasonable to choose for p.addParamValue here (using the inputParser p). The default value, however, is a vector of zeros of length n. Here n is passed as a required argument. How can I use this required parameter if it hasn't been parsed yet? Or is there a better way to handle this kind of input?
Akzeptierte Antwort
Weitere Antworten (2)
Nathaniel
am 21 Sep. 2012
function yourFunction(n, varargin)
p = inputParser;
p.addRequired('n', @(x)(true));
p.addParamValue('opt1', zeros(1,n), @(x)(true));
p.parse(n, varargin{:});
% do something useful
end
Kategorien
Mehr zu Argument Definitions finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!