Input parsing of name-value Pairs
32 views (last 30 days)
Show older comments
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 Comment
Stephen23
on 18 Oct 2017
Have a look at my FEX submission num2words for one easy way to parse key-value pairs. In particular the local function n2wOptions and the functions it calls: you could easily adapt this to use any UI dialog box.
Accepted Answer
OCDER
on 18 Oct 2017
You could make your own input-parser that searches for a string within varargin, and if it does not exist, ask users. Try the following examples:
Out = exfunction('dog', 'Param1', 10.23, 'Param2', 'cat') %All param-value pairs are defined
Out = exfunction('dog') %Asks user for missing param-value pairs
Here, exfunction uses the parseInputVar to handle param-value pairs and ask users for missing pairs.
function Out = exfunction(arg1, varargin)
Param1 = parseInputVar('Param1', varargin{:});
Param2 = parseInputVar('Param2', varargin{:});
Out = {arg1, Param1 Param2};
function Value = parseInputVar(ParamName, varargin)
VarLoc = find(strcmpi(varargin, ParamName));
if ~isempty(VarLoc)
Value = varargin{VarLoc(1)+1}; %Get the value after the param name
else
InputValue = inputdlg(sprintf('What is "%s"?', ParamName));
Value = sscanf(InputValue{1}, '%f', 1);
if isempty(Value)
Value = InputValue{1};
end
end
If you want this function to be robust, you could add some more input-checking features into the input parsing function.
0 Comments
More Answers (2)
Steven Lord
on 18 Oct 2017
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.
2 Comments
Elimelech Schreiber
on 29 Jan 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.
0 Comments
See Also
Categories
Find more on Argument Definitions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!