Main Content

nargchk

Validate number of input arguments

nargchk is not recommended. Use narginchk instead.

Description

example

msgText = nargchk(minArgs,maxArgs,numArgs) validates the number of input arguments and returns a message if the number of inputs, numArgs, is fewer than minArgs or greater than maxArgs.

This syntax is the same as msgText = nargchk(minArgs,maxArgs,numArgs,'string').

example

msgStruct = nargchk(minArgs,maxArgs,numArgs,'struct') returns a message structure instead of a character vector.

Examples

collapse all

In a file named checkInputs, create a function that uses nargchk to verify that the function has been called with a valid number of inputs.

function checkInputs(varargin)
    msgTxt = nargchk(2,3,nargin)
end

Call the checkInputs function with a valid number of inputs. nargchk returns an empty character vector.

checkInputs(13,7)
msgTxt =

     []

Call the checkInputs function with too few inputs.

checkInputs(42)
msgTxt =

    'Not enough input arguments.'

Call the checkInputs function with too many inputs.

checkInputs(0,1,1,2,3)
msgTxt =

    'Too many input arguments.'

In a file named checkInputs, create a function that uses nargchk with the 'struct' parameter to verify that the function has been called with a valid number of inputs.

function checkInputs(varargin)
    msgStruct = nargchk(2,3,nargin,'struct');
    error(msgStruct)
end

At the command prompt, call the checkInputs function with an accepted number of inputs. nargchk does not throw an error.

checkInputs(13,7)

Call the checkInputs function with too few inputs.

checkInputs(42)
Error using checkInputs (line 3)
Not enough input arguments.

Call the checkInputs function with too many inputs.

checkInputs(0,1,1,2,3)
Error using checkInputs (line 3)
Too many input arguments.

Input Arguments

collapse all

Minimum number of accepted inputs, specified as a scalar.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Maximum number of accepted inputs, specified as a scalar.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Number of function inputs, specified as a scalar. Typically, you use the nargin function to determine the number of input arguments specified in the function call.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

collapse all

Message text, returned as 'Not enough input arguments.', 'Too many input arguments.', or an empty matrix.

If numArgs is less than minArgs, then nargchk returns the character vector 'Not enough input arguments.' If numArgs is greater than maxArgs, then nargchk returns the character vector 'Too many input arguments.' Otherwise, nargchk returns an empty matrix.

Message and identifier, returned as a structure with message and identifier fields. If numArgs is less than minArgs, then nargchk returns this structure:

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

If numArgs is greater than maxArgs, then nargchk returns this structure:

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

Otherwise, nargchk returns an empty structure.

Tips

  • nargchk is often used with the error function. The error function accepts either type of return value from nargchk: a message character vector or message structure. For example, this command uses the output message structure from nargchk as the input to the error function.

    error(nargchk(2,4,nargin,'struct'))

    If the number of input arguments is within the expected range, then nargchk returns an empty character vector or structure. When the error function receives an empty character vector or structure, it does not throw an error.

Introduced before R2006a