Input parsing of name-value Pairs
30 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2017-10-18
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.
采纳的回答
OCDER
2017-10-18
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 个评论
更多回答(2 个)
Steven Lord
2017-10-18
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 个评论
Elimelech Schreiber
2019-1-29
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Argument Definitions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!