addRequired - Add required, positional argument into input parser scheme - MATLAB (original) (raw)
Add required, positional argument into input parser scheme
Syntax
Description
addRequired([p](#d126e896480),[argName](#d126e896502))
adds a required, positional input argument argName
into the input parser scheme p
.
addRequired([p](#d126e896480),[argName](#d126e896502),[validationFcn](#bs8m6a1%5Fsep%5Fshared-validationFcn))
specifies a validation function for the input argument.
Examples
Create an inputParser
object and add a required input named myinput
to the input scheme.
p = inputParser; argName = 'myinput'; addRequired(p,argName)
Call the parse
function with the input value 7, and display the results.
ans = struct with fields: myinput: 7
Create an input parser scheme that checks that a required input is a nonnegative, numeric scalar. The syntax @(x)
creates a handle to an anonymous function with one input.
p = inputParser; argName = 'num'; validationFcn = @(x) (x > 0) && isnumeric(x) && isscalar(x); addRequired(p,argName,validationFcn)
Parse an invalid input, such as -1
:
The value of 'num' is invalid. It must satisfy the function: @(x)(x>0)&&isnumeric(x)&&isscalar(x).
Create an inputParser
object and define a validation function using validateattributes
. The validation function tests that a required input is numeric, positive, and even.
p = inputParser; argName = 'evenPosNum'; validationFcn = @(x) validateattributes(x,{'numeric'},... {'even','positive'}); addRequired(p,argName,validationFcn)
Parse an input character vector. Parse fails because the input is invalid.
The value of 'evenPosNum' is invalid. Expected input to be one of these types:
double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64
Instead its type was char.
Parse an odd number. Parse fails because the input is invalid.
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
Data Types: function_handle
Version History
Introduced in R2007a