inputparser: error when using a structure as a surrogate for parameter-value pairs.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Umberto
le 25 Mai 2015
Modifié(e) : Ashish Gudla
le 26 Mai 2015
I have a function that has a Required input, an Optional one and a Parameter-Value pair. I was trying to substitute the ParamValue pair with a structure, with StructExpand = true.
So let's say for example, if the function has
y = myfun(varargin)
p = inputParser;
addRequired(p, 'a', @isnumeric);
addOptional(p, 'b', 1, @isscalar);
addParameter(p, 'c', 1, @isscalar);
parse(p, varargin{:})
...
If I call myfun(5, 'c', 2) there are no problems. But if I try to substitute myfun(5, 'c', 2) by defining st.c = 2 and then calling myfun(5, st), it gives me an error. The function works only if I specify also the Optional input and then the structure as in myfun(5, 1, st). Do you know why? Maybe the function reads st as 'b' input? How can I deal with this problem?
Thanks
0 commentaires
Réponse acceptée
Ashish Gudla
le 26 Mai 2015
Modifié(e) : Ashish Gudla
le 26 Mai 2015
It would work when you define 'c' in a structure as well. Please see the example below:
function myfun(varargin)
p = inputParser;
p.StructExpand=1;
addRequired(p,'a',@isnumeric);
addOptional(p,'b',1,@isnumeric);
addParameter(p,'c',1,@isnumeric);
parse(p,varargin{:});
p.Results
end
>> myfun(5,'c',2)
ans =
a: 5
b: 1
c: 2
>> st.c=2;
>> myfun(5,st)
ans =
a: 5
b: 1
c: 2
What is the error you are getting? It may be unrelated to the issue you explained.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Argument Definitions 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!