Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "addOptional" and "addParameter" functions in MATLAB?

What is the difference between addOptional and addParameter in MATLAB for creating functions?

MATLAB documention of these two functions:

addOptional(p,argName,default) adds optional input, argName, to the input parser scheme of inputParser object, p. When the inputs that you are checking do not include a value for this optional input, the input parser assigns the default value to the input.

addOptional(p,argName,default,validationFcn) specifies a validation function for the input argument.

addParameter(p,paramName,default) adds parameter name and value argument paramName to the input parser scheme of inputParser object, p. When the inputs that you are checking do not include a value for this optional parameter, the input parser assigns the default value.

addParameter(p,paramName,default,validationFcn) specifies a validation function for the input argument.

like image 771
Eghbal Avatar asked Nov 25 '25 07:11

Eghbal


1 Answers

addParameter adds a Parameter/Value pair to the input syntax of your function. For example, if you had a function called myFunction in which you were using the input parser:

addRequired(p,'x')
addParameter(p,'Foo',1)

Would add:

myFunction(x,'Foo',value)

As a valid syntax with a default value of 1. In parameter value pairs, the name of the parameter is specified with a string or character array, followed by a value specification.

addOptional(p,'Foo',value)

Would add:

myFunction(x,value)

As an optional positional argument. In this case, you only specify the value of the optional argument without specifying a parameter name.

like image 172
Alex Taylor Avatar answered Nov 26 '25 23:11

Alex Taylor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!