This is machine translation

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


Validate number of output arguments


  • nargoutchk(minargs, maxargs)
  • msgtxt = nargoutchk(minargs,maxargs,numargs)
  • msgstruct = nargoutchk(minargs,maxargs,numargs,'struct')


nargoutchk(minargs, maxargs) throws an error if the number of outputs specified in the call is less than minargs or greater than maxargs. If the number of outputs is between minargs and maxargs (inclusive), nargoutchk does nothing.

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

       message: 'Not enough output arguments.'
    identifier: 'MATLAB:nargoutchk:notEnoughOutputs'

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

       message: 'Too many output arguments.'
    identifier: 'MATLAB:nargoutchk:tooManyOutputs'

msgtxt = nargoutchk(minargs,maxargs,numargs) returns the error message as a character vector if the number of outputs specified in the call, numargs, is less than minargs or greater than maxargs. If numargs is between minargs and maxargs (inclusive), nargoutchk returns an empty matrix.

    Note:   nargoutchk(minargs,maxargs,numargs) will not accept numargs in a future release.

msgstruct = nargoutchk(minargs,maxargs,numargs,'struct') returns the error message and identifier as a structure.

    Note:   nargoutchk(minargs,maxargs,numargs,'struct') will not accept numargs or 'struct' in a future release.


This function uses nargoutchk to verify that a minimum of 2 and maximum of 5 input arguments are passed back to the calling function:

function [varargout] = check_outputs(array_in)
minargs=2;  maxargs=5;

% Number of outputs must be >=minargs and <=maxargs.
nargoutchk(minargs, maxargs)

for k=1:nargout
    varargout{k} = array_in(k)*3;

Initialize input array X to a vector of 6 elements:

X = 5:7:40
X =
     5    12    19    26    33    40

Call the example function with 1 output argument. This is less than the minimum (2) that was specified by nargoutchk and results in an error:

A = check_outputs(X);

Error using check_outputs
Not enough output arguments.

Call the function with 4 output arguments. This is within the allowable bounds (2 to 5) specified by nargoutchk:

[A, B, C, D] = check_outputs(X);

[A, B, C, D]
ans =
    15    36    57    78

Call the function with 6 output arguments. This exceeds the maximum (5) that was specified by nargoutchk and results in an error:

[A, B, C, D, E, F] = check_outputs(X);

Error using check_outputs
Too many output arguments.

More About

collapse all


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

Introduced before R2006a

Was this topic helpful?