Skip to Main Content Skip to Search
Product Documentation

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.

See Also

nargin | nargout | nargoutchk | varargin | varargout

  


Free MATLAB Interactive Kit

Explore how to use MATLAB to make advancements in engineering and science.


Download free kit

Trials Available

Try the latest version of MATLAB and other MathWorks products.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS