Input parsing of name-value Pairs

31 visualizaciones (últimos 30 días)
Rafael Kübler
Rafael Kübler el 18 de Oct. de 2017
Comentada: per isakson el 27 de Jun. de 2019
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 comentario
Stephen23
Stephen23 el 18 de Oct. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

OCDER
OCDER el 18 de Oct. de 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.

Más respuestas (2)

Steven Lord
Steven Lord el 18 de Oct. de 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 comentarios
Rafael Kübler
Rafael Kübler el 20 de Oct. de 2017
Thanks for this answer. I have read about the intputParser now and i think it certainly is what I'm looking for. But I'm not sure, how to implement it exactly. Could you give me a code example, where to place the inputdlg so i does not appear, when the corresponding input name is in varargin?

Iniciar sesión para comentar.


Elimelech Schreiber
Elimelech Schreiber el 29 de En. de 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.

Categorías

Más información sobre Argument Definitions en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by