This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Class: inputParser

Add required positional argument to input parser scheme




addRequired(p,argName) adds required argument, argName, to the input parser scheme of inputParser object, p.

addRequired(p,argName,validationFcn) includes a validation function for the input argument.

Input Arguments


Object of class inputParser.


String scalar or character vector that specifies the internal name for the input argument.

Arguments added with addRequired are positional. When you call a function with positional inputs, you must specify inputs in the order that they are added to the input parser scheme.


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.


expand all

Create an inputParser object and add a required input named myinput to the input scheme.

p = inputParser;
argName = 'myinput';

Check whether a required input named num is a numeric scalar greater than zero.

p = inputParser;
argName = 'num';
validationFcn = @(x) isnumeric(x) && isscalar(x) && (x > 0);

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

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

The value of 'num' is invalid. It must satisfy the function: @(x)isnumeric(x)&&isscalar(x)&&(x>0).

Create an inputParser object and define a validation function using validateattributes to test that required input is numeric, positive, and even. Add the required input to the scheme.

p = inputParser;
argName = 'evenPosNum';
validationFcn = @(x) validateattributes(x,{'numeric'},...

Parse an input character vector. Parse will fail.

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 will fail.

The value of 'evenPosNum' is invalid. Expected input to be even.

Parse an even, positive number. Parse will pass.


Introduced in R2007a

Was this topic helpful?