Allowing unknown parameters in an inputParser
Mostrar comentarios más antiguos
Hello, I am attempting to figure out a better way of handling parameters and passing them through input parsers. Let us say I have three parameters, and I will store them in a struct, and then pass that struct as the arguments to multiple input parsers that DON'T explicitly include the stated variables without it throwing errors.
params = struct(...
'type','fruit',...
'name','banana',...
'price',3.14)
Ok, now lets say I want to hand this as the parameters to two different functions. This first one sets a default price for some reason,
function tot = myFun1(n,varargin)
defaultPrices = struct(...
'apple',ln(1),...
'banana',sqrt(-1),...
'orange',3.14);
p = inputParser
addParameter(p,'name','orange',@(x) ismember(x,{'apple','orange','banana'}));
addParameter(p,'price',[])
p = parse(p,varargin{:})
price = p.Results.price;
if isempty(price)
price = defaultPrices.(p.Results.name);
end
tot = n*price;
end
and this second one lets say pulls up a picture.
function myFun2(varargin)
defaultImage = 'ErrorScreen.png'
p = inputParser;
addParameter(p,'type',[]);
addParameter(p,'name',[]);
parse(p,varargin{:})
try
im = imload(fullfile(pwd,p.Results.type,p.Results.name));
catch
im = imload(defaultImage);
end
imshow(im);
end
Ok, now for the two functions above I want to be able to call both with the same params structure:
tot = myFun1(1E6,params);
myFun2(params);
However, this will lead to an error in both functions; the first because 'type' is not explicitly included in the input parser, "'type' is not a recognized parameter. For a list of valid name-value pair arguments, see the documentation for this function.", and similarly for myFun2 due to 'price.
So, my question is whether (A) there is a way to get the parser to be more tolerant of unexpected input fields, so I can do the above, or (B) if there is some other "best practices" method so that I can avoid creating multiple params structures with redundant fields, or duplicating the structure, and then cycling through 'rmfield's before input. Neither is terribly elegant, and I am trying to avoid lots of 'addParameter's for params that I don't actually need in a specific function just to avoid the error.
The above is just a goofy MWE of what I am trying to code, help is appreciated.
2 comentarios
D. Plotnick
el 16 de Abr. de 2018
Editada: D. Plotnick
el 16 de Abr. de 2018
D. Plotnick
el 16 de Abr. de 2018
Respuesta aceptada
Más respuestas (1)
(B) if there is some other "best practices" method so that I can avoid creating multiple params structures with redundant fields
An alternative would be to use a tree of nested structures,
allParams.myFun1.name =... allParams.myFun1.price =...
allParams.myFun2.name =... allParams.myFun2.type =...
and make function calls like
tot = myFun1(1E6,allParams.myFun1); myFun2(allParams.myFun2)
This involves no more storage than the total number of default parameters across the different functions. It also avoids name conflict between functions with the same parameter names.
Categorías
Más información sobre Argument Definitions en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!