Input parsing of name-value Pairs
Mostra commenti meno recenti
Hello together,
i know its a heavy discussed topic but i haven't found a satisfying answer to my problem yet:
my function call should be as flexible as possilble, so it looks like this:
function example(arg1, varargin)
varargin may contain several name-value pairs in different order.
the problem with the solutions out there is, that they all set a default value, when a name is not found in the function call. But instead of setting a default value, i'd like get the value of the not found name by using a inputdlg. Whenn the name is found in varargin it should not open the inputdlg and instead use the name-value pair syntax.
Is there any compact and robus way to do this?
Thank you for your help.
Rafael
1 Commento
Risposta accettata
Più risposte (2)
Steven Lord
il 18 Ott 2017
0 voti
If you're using an inputParser object the UsingDefaults property sounds like exactly what you want. Iterate over the elements of the cell array stored as the UsingDefaults property and open an inputdlg asking the user to enter the value of that input. You can't update the Results property of the inputParser object itself, as that's read-only, but you could retrieve that property into a regular struct (that is not a property of the object) and have the code that uses inputdlg update that struct.
Elimelech Schreiber
il 29 Gen 2019
I would use
addParameter(p,paramName,defaultVal)
where
defaultVal = []% or: defaultVal = 'AskUser'
and then, after name-value parsing:
if isempty(paramName) % or: strcmp(paramName,'AskUser')
paramName = inputdlg(...);
end
You might then want to validate the input again.
Categorie
Scopri di più su Argument Definitions in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!