Input parsing of name-value Pairs
조회 수: 46 (최근 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!