There are various ways for implementing name-value arguments.
Some of the ways are as follows:
function res = func(a, b, options)
arguments
a
b
options.c = 1
options.d = 1
end
disp(a);disp(b);disp(options.c);disp(options.d);
end
Now, user can use the following syntaxes:
func(1,2);
func(1,2,'c',4);
func(1,2,'d',3);
func(1,2,'c',3,'d',5);
function res = func(a, b, varargin)
for ind=1:2:numel(varargin)
name=varargin{ind};
val=varargin{ind+1};
end
inp = inputParser;
addParameter(inp, 'argName1', defaultValue, validationFcn);
addParameter(inp, 'argName2', defaultValue, validationFcn);
addParameter(inp, 'argName3', defaultValue, validationFcn);
parse(inp, varargin{:});
arg1 = inp.Results.argName1;
arg2 = inp.Results.argName2;
end
Now, user syntaxes:
func(1, 2, 'a', 2);
func(1, 2, 'c', 3, 'd', 4);
So, these were some ways to implement (optional) name-value arguments.
Hope it helps!