addOptional - Add optional, positional argument into input parser scheme - MATLAB (original) (raw)
Add optional, positional argument into input parser scheme
Syntax
Description
addOptional([p](#d126e895567),[argName](#d126e895589),[defaultVal](#bs8nb4r%5Fsep%5Fshared-default))
adds an optional, positional input argument, argName
, into the input parser scheme p
. When the inputs to a function do not include a value for this optional input, the input parser assigns it the valuedefaultVal
.
addOptional([p](#d126e895567),[argName](#d126e895589),[defaultVal](#bs8nb4r%5Fsep%5Fshared-default),[validationFcn](#bs8nb4r%5Fsep%5Fshared-validationFcn))
specifies a validation function for the input argument.
Examples
Create an inputParser
object and add an optional input to the input parser scheme. Name the argument myinput
, and assign it a default value of 13.
p = inputParser; argName = 'myInput'; defaultVal = 13; addOptional(p,argName,defaultVal)
Call the parse function with no inputs, and display the results.
ans = struct with fields: myInput: 13
Call the parse
function with an input value of 42, and display the results.
ans = struct with fields: myInput: 42
Validate that an optional input named num
, with a default value of 1, is a numeric scalar greater than zero.
Create an input parser scheme. For the validation function,@(x)
creates a handle to an anonymous function that accepts one input.
p = inputParser; argName = 'num'; defaultVal = 1; validationFcn = @(x) isnumeric(x) && isscalar(x) && (x > 0); addOptional(p,argName,defaultVal,validationFcn)
Parse an invalid input argument, such as -1
.
The value of 'num' is invalid. It must satisfy the function: @(x)isnumeric(x)&&isscalar(x)&&(x>0).
Define a validation function usingvalidateattributes
. Validate that an argument is numeric, positive, and even.
validationFcn = @(x) validateattributes(x,{'numeric'},... {'even','positive'});
Create an input parser scheme that includes an optionalevenPosNum
argument with a default value of 1. Validate the input argument with validationFcn
.
p = inputParser; argName = 'evenPosNum'; defaultVal = 1; addOptional(p,argName,defaultVal,validationFcn)
Parse an input string. Parse fails.
The value of 'evenPosNum' is invalid. Expected input to be one of these types:
numeric
Instead its type was string.
Parse an odd number. Parse fails.
The value of 'evenPosNum' is invalid. Expected input to be even.
Parse an even, positive number. Parse passes.
Input Arguments
Input parser scheme, specified as an inputParser
object.
Name of the input argument, specified as a character vector or string scalar.
Example: 'firstName'
Example: 'address'
Data Types: char
| string
Default value for the input, specified as any data type. If argName
is not an input to the function, when the parse
function parses the inputs, then it assigns argName
the value defaultVal
.
Data Types: function_handle
Tips
- Arguments added to the input parser scheme with the
addOptional
function are positional. Therefore, add them to the input parser scheme in the same order they are passed into the function. - For optional string arguments, specify a validation function. Without a validation function, the input parser interprets a string argument as an invalid parameter name and throws an error.
- Use
addOptional
to add an individual argument into the input parser scheme. If you want to parse an optional name-value pair, then use theaddParameter
function.
Version History
Introduced in R2007a