addParamValue - (Not recommended) Add optional name-value pair argument into input parser scheme - MATLAB (original) (raw)
(Not recommended) Add optional name-value pair argument into input parser scheme
Syntax
Description
addParamValue([p](#d126e896187),[paramName](#d126e896209),[defaultVal](#bs8m%5Foy%5Fsep%5Fshared-default))
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 valuedefaultVal
.
Unlike positional inputs added with the addRequired
andaddOptional
functions, each parameter added withaddParamValue
corresponds to two input arguments: one for the name and one for the value.
addParamValue([p](#d126e896187),[paramName](#d126e896209),[defaultVal](#bs8m%5Foy%5Fsep%5Fshared-default),[validationFcn](#bs8m%5Foy%5Fsep%5Fshared-validationFcn))
specifies a validation function for the input argument.
Examples
Validate that the value corresponding tomyParam
, 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
.
The value of 'myparam' is invalid. Value must be positive, scalar, and numeric.
Input Arguments
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
.
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