Input parser: default argument depends on another required argument, how to do this?
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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?
0 Commenti
Risposta accettata
Daniel Shub
il 21 Set 2012
Modificato: Daniel Shub
il 21 Set 2012
This is where the UsingDefaults property of the inputParser object is useful. Assuming an inputParser object p, a required property n, and an optional property optProp
if any(strcmp(p.UsingDefaults, 'optProp'))
optProp = zeros(1, n);
end
2 Commenti
Daniel Shub
il 24 Set 2012
That is your opinion. Much of the MATLAB codebase is based on fixed order optional arguments. Consider the min function. The current syntax is min(X,[],2). Using PV pairs would give you min(x, 'Dimension', 2), but then you might have the case of min(1:9, 'Dimension') which probably should raise an error about the number of arguments, but cannot unless you want more magic words in MATLAB.
Più risposte (2)
Vedere anche
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!