Documentation Center

  • Trial Software
  • Product Updates

narginchk

Validate number of input arguments

Syntax

narginchk(minargs, maxargs)

Description

narginchk(minargs, maxargs) throws an error if the number of inputs specified in the call to the currently executing function is less than minargs or greater than maxargs. If the number of inputs is between minargs and maxargs (inclusive), narginchk does nothing.

When too few inputs are supplied, the message identifier and message are:

    identifier: 'MATLAB:narginchk:notEnoughInputs'
       message: 'Not enough input arguments.'

When too many inputs are supplied, the message identifier and message are:

    identifier: 'MATLAB:narginchk:tooManyInputs'
       message: 'Too many input arguments.'

Examples

This function uses narginchk to verify that a minimum of 2 and maximum of 5 input arguments are received from the calling function:

function check_inputs(A, B, varargin)
minargs=2;  maxargs=5;

% Number of inputs must be >=minargs and <=maxargs.
narginchk(minargs, maxargs)

fprintf('Received 2 required, %d optional inputs.\n\n', ...
    size(varargin, 2))

Call the example function, passing 1 input argument:

check_inputs(23)
Error using check_inputs
Not enough input arguments.

Call the function, passing 5 arguments:

check_inputs(23, 9, 15, 34, 62)
Received 2 required, 3 optional inputs.

Call the function, passing 6 arguments:

check_inputs(23, 9, 15, 34, 62, 6)
Error using check_inputs
Too many input arguments.

More About

expand all

Tips

  • To verify that there are a minimum of N arguments, specify inf as maxargs. For example: narginchk(5,inf) throws an error when there are not at least five inputs.

See Also

| | | |

Was this topic helpful?