MATLAB: Input parsing of name-value Pairs

MATLABname-value pairs;parse

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

Best Answer

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.