addRequired
Add required, positional argument into input parser scheme
Description
Examples
Add Required Input
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.
parse(p,7) p.Results
ans = struct with fields:
myinput: 7
Validate Required Input Is Nonnegative
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
:
parse(p,-1)
The value of 'num' is invalid. It must satisfy the function: @(x)(x>0)&&isnumeric(x)&&isscalar(x).
Validate Required Input Using validateattributes
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.
parse(p,'hello')
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.
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
p
— Input parser scheme
inputParser
object
Input parser scheme, specified as an inputParser
object.
argName
— Name of input argument
character vector | string scalar
Name of the input argument, specified as a character vector or string scalar.
Example: 'firstName'
Example: 'address'
Data Types: char
| string
validationFcn
— Function to validate argument
function handle
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
Version History
Introduced in R2007a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)