addRequired

Adds a required positional argument to an inputParser.

Category: Programming > Functions

Syntax

addRequired(p, name)

Declares the argument name.

addRequired(p, name, validationFcn)

Also checks the value with a validation function.

Description

Required arguments come first in the call and are matched by position, in the order they were added. If the call supplies too few, parse stops with an error. After parse, the value is available as a field of p.Results under the given name.

Add required arguments before optional ones and name-value pairs.

Input arguments

p
An inputParser object.
name
Argument name, as text.
validationFcn
Handle to a function that returns true for a valid value, or errors.

Example

A validated required argument

p = inputParser;
addRequired(p, 'n', @isnumeric);
parse(p, 7);
p.Results.n
try
    parse(p, 'text');
catch err
    disp(err.message)
end
Output
ans = 7
The value of 'n' is invalid. It must satisfy the function: isnumeric.

See also

inputParser addOptional addParameter validateattributes