Equivalent of passing structure to “varargin” for the new “Function Argument Validation” introduced in 2019b
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!
-NateI 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 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 inputparser, varargin, function argument validation, arguments MATLAB Answers — New Questions