Documentation

addOptional

Add optional, positional argument into input parser scheme

Syntax

addOptional(p,argName,defaultVal)
addOptional (p argName defaultVal validationFcn)

Description

example

addOptional(p,argName,defaultVal)adds an optional, positional input argument,argName, into the input parser schemep. When the inputs to a function do not include a value for this optional input, the input parser assigns it the valuedefaultVal.

example

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

Examples

collapse all

Create aninputParserobject and add an optional input to the input parser scheme. Name the argumentmyinput, 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.

parse(p) p.Results
ans =struct with fields:myInput: 13

Call theparsefunction with an input value of 42, and display the results.

parse(p,42) p.Results
ans =struct with fields:myInput: 42

Validate that an optional input namedνm, 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.

parse(p,-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 optionalevenPosNumargument with a default value of 1. Validate the input argument withvalidationFcn.

p = inputParser; argName ='evenPosNum'; defaultVal = 1; addOptional(p,argName,defaultVal,validationFcn)

Parse an input string. Parse fails.

parse(p,"hello")
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.

parse(p,13)
The value of 'evenPosNum' is invalid. Expected input to be even.

Parse an even, positive number. Parse passes.

parse(p,42)

Input Arguments

collapse all

Input parser scheme, specified as aninputParserobject.

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. IfargNameis not an input to the function, when theparsefunction parses the inputs, then it assignsargNamethe valuedefaultVal.

函数validate an argument, specified as a function handle.

The function handle must be associated with a function that returnstrueorfalse, or passes a test, or throws an error. Both types of functions must accept a single input argument.

Example:@(s)isstring(s)

Example:@(x)isnumeric(x)&&isscalar(x)

Example:@(n)validateattributes(n,{'numeric'},{'nonnegative'})

Data Types:function_handle

Tips

  • Arguments added to the input parser scheme with theaddOptionalfunction 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.

  • UseaddOptionalto add an individual argument into the input parser scheme. If you want to parse an optional name-value pair, then use theaddParameterfunction.

Introduced in R2007a

Was this topic helpful?