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

8 views (last 30 days)
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.
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;
% Rest of function.
I could call it like this:
Or, to make the function call more compact, I could call it like this:
S.param1 = 7;
S.param2 = 8;
S.param3 = 9;
With the second method, inputParser would interpret the fields of the structure as the name-value pairs.
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)
NameValueArgs.param1 = 1.5;
NameValueArgs.param2 = -8;
NameValueArgs.param3 = 17;
% Rest of function.
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!

Answers (0)


Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by