Main Content

addParamValue

(Not recommended) Add optional name-value pair argument into input parser scheme

addParamValue is not recommended. Use addParameter instead.

Description

addParamValue(p,paramName,defaultVal) adds the parameter name of an optional name-value pair argument into the input parser scheme. When the inputs to a function do not include this optional name-value pair, the input parser assigns paramName the value defaultVal.

Unlike positional inputs added with the addRequired and addOptional functions, each parameter added with addParamValue corresponds to two input arguments: one for the name and one for the value.

example

addParamValue(p,paramName,defaultVal,validationFcn) specifies a validation function for the input argument.

Examples

collapse all

Validate that the value corresponding to myParam, 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;
paramName = 'myParam';
defaultVal = 1;
errorMsg = 'Value must be positive, scalar, and numeric.'; 
validationFcn = @(x) assert(isnumeric(x) && isscalar(x) ...
    && (x > 0),errorMsg);
addParamValue(p,paramName,defaultVal,validationFcn)

Parse an invalid input argument, such as -1.

parse(p,'myparam',-1)
The value of 'myparam' is invalid. Value must be positive, scalar, and numeric.

Input Arguments

collapse all

Input parser scheme, specified as an inputParser object.

Name of the input parameter, 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.

Function to validate an argument, specified as a function handle.

The function handle must be associated with a function that returns true or false, 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

  • Parameter name-value pairs are optional inputs. When calling the function, name-value pairs can appear in any order after positional arguments. They take the general form Name1,Value1,...,NameN,ValueN.

Version History

Introduced in R2007a