Input parser: default argument depends on another required argument, how to do this?

9 vues (au cours des 30 derniers jours)
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?

Réponse acceptée

Daniel Shub
Daniel Shub le 21 Sep 2012
Modifié(e) : Daniel Shub le 21 Sep 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
While you can use inputParser to do what you want, there are a number of other ways of doing it.
  2 commentaires
Folkert
Folkert le 24 Sep 2012
Thanks! In the link you gave, the order of arguments is important. If there are three optional arguments, suppose the user only wants to pass the 3rd argument. Should he give two dummy arguments containing nonsense only to pass a 3rd? Here parameter-value pairs are much better. What is the sense of having varargin in the argument list when the order of arguments is fixed? Then it is better to name all optional variables (treat them as required) and give them default values if their values are not passed by the function call. Then at least you know the argument names...
Daniel Shub
Daniel Shub le 24 Sep 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.

Connectez-vous pour commenter.

Plus de réponses (2)

Nathaniel
Nathaniel le 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

Folkert
Folkert le 24 Sep 2012
Modifié(e) : Folkert le 24 Sep 2012
Another way:
Call the following function as:
(1, 2, 'speed', 2, 'direction', [6, 1]);
(varargin is composed of params-value pairs)
function foo(a,b, varargin)
% default value structure:
default.speed = 10;
default.direction = [2,3];
opt = struct(varargin{:});
opt = setstructfields(default, opt);
@Daniel, you're right, it's just a matter of opinion. Each solution has advantages and disadvantages.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by