Quantcast

Documentation Center

  • Trial Software
  • Product Updates

addParamValue

Class: inputParser

(Not recommended) Add parameter name and value argument to Input Parser scheme

addParamValue is not recommended. Use addParameter instead.

Syntax

addParamValue(p,paramName,default)
addParamValue(p,paramName,default,validationFcn)

Description

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

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

Tips

  • If your parameter values do not require validation, you do not have to include them in the input scheme with addParamValue. As an alternative, set the KeepUnmatched property of the inputParser object to true. The parser stores extra parameter names and values in the Unmatched property rather than in the Results property of the object. For an example, see the inputParser reference page.

Input Arguments

p

Object of class inputParser.

paramName

String that specifies the internal name for the input parameter.

Parameter names and values are optional inputs. When calling the function, name and value pairs can appear in any order, with the general form Name1,Value1,...,NameN,ValueN.

default

Default value for the input. This value can be of any data type.

validationFcn

Handle to a function that checks if the input argument is valid.

inputParser accepts two types of validation functions: functions that return true or false, and functions that pass or throw an error. Both types of validation functions must accept a single input argument.

Examples

expand all

Add Optional Parameter Value Input

Create an inputParser object and add an optional input named myparam with a default value of 0 to the input scheme.

p = inputParser;
paramName = 'myparam';
default = 0;
addParameter(p,paramName,default);

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

Pass both the parameter name and value to the parse method.

parse(p,'myparam',100);
p.Results
ans = 

    myparam: 100

Validate Parameter Value

Check whether the value corresponding to myparam is a numeric scalar greater than zero.

p = inputParser;
paramName = 'myparam';
default = 1;
validationFcn = @(x) isnumeric(x) && isscalar(x) && (x > 0);
addParamValue(p,paramName,default,validationFcn);

The syntax @(x) creates a handle to an anonymous function with one input.

Attempt to parse an invalid value, such as -1:

parse(p,'myparam',-1)
Argument 'myparam' failed validation @(x)isnumeric(x)&&isscalar(x)&&(x>0).

See Also

| | |

More About

Was this topic helpful?