Equivalent of passing structure to "varargin" for the new "Function Argument Validation" introduced in 2019b

17 visualizaciones (últimos 30 días)
I have a compatibility question about the new method of adding name-value pairs to functions introduced in MATLAB 2019b as discussed on this help page. I think the new argument validation tools are very powerful and easier to use, but there's a special use case for varargin and inputParser that I can't get to work.
Background
In previous versions of MATLAB, I've used varargin with inputparser to enable name-value pairs in functions. I only used these as optional arguments, so they were always at the end of my function signature and I only used addParameters. With inputParser, I am able to pass either the variables in, or a structure with field names that correspond to the variables. For the sample function:
function demo(varargin)
% Parse the input.
p = inputParser;
p.addParameter('param1',1.5);
p.addParameter('param2',-8);
p.addParameter('param2',17);
p.parse(varargin{:});
% Rest of function.
...
end
I could call it like this:
demo('param1',7,'param2',8,'param3',9);
Or, to make the function call more compact, I could call it like this:
S.param1 = 7;
S.param2 = 8;
S.param3 = 9;
demo(S);
With the second method, inputParser would interpret the fields of the structure as the name-value pairs.
Question
With the new function argument validation tools, I can eliminate the need for varargin and inputparser and use the built in tools:
function demo(NameValueArgs)
arguments
NameValueArgs.param1 = 1.5;
NameValueArgs.param2 = -8;
NameValueArgs.param3 = 17;
end
% Rest of function.
...
end
However, I can't pass a structure with the name value pairs like I can with varargin and inputparser, it produces an error.
Is there a way to get the "old" functionality back using the new arguments feature of functions? For functions with many name-value pairs, the function calls can get very long otherwise.
Thanks in advance!
-Nate

Respuestas (1)

Tejas
Tejas el 25 de Abr. de 2024
Hello Nate,
It seems you are looking to streamline function calls by passing input arguments as name-value pairs in the form of a structure. This approach aims to make the function call more compact and convenient.
However, the new approach of function argument validation, expects inputs to be in the form of name-value pairs. Consequently, providing a structure directly as an input will cause an issue.
To pass structure as an input, I would like to suggest a workaround:
  • Create a structure containing name-value pairs as per requirements.
S = struct();
S.param1 = 1.5;
S.param2 = -8;
S.param3 = 17;
  • Convert this structure into name-value pairs and store them in a cell array within a variable.
S = reshape([fieldnames(S), struct2cell(S)].', 1, []);
  • Pass the contents of this variable as the input to function.
demo(S{:});
function demo(NameValueArgs)
arguments
NameValueArgs.param1 (1,1) double = 0;
NameValueArgs.param2 (1,1) double = 0;
NameValueArgs.param3 (1,1) double = 0;
end
result = NameValueArgs.param1 + NameValueArgs.param2 + NameValueArgs.param3;
disp(result);
end
Here is a screenshot showcasing what the output looks like.
The file has been named argsValidation.m.
Please refer to below documentations for better understanding of functions used in the workaround:
Hope it helps!

Categorías

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

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by