Documentation

This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

nargoutchk

Validate number of output arguments

Syntax

nargoutchk(minArgs,maxArgs)
msgText = nargoutchk(minArgs,maxArgs,numArgs)
msgStruct = nargoutchk(minArgs,maxArgs,numArgs,'struct')

Description

example

nargoutchk(minArgs,maxArgs) validates the number of output arguments. nargoutchk throws an error if the number of outputs specified in the function call is less than minArgs or greater than maxArgs. If the number of outputs is between minArgs and maxArgs (inclusive), nargoutchk does nothing.

msgText = nargoutchk(minArgs,maxArgs,numArgs) validates the value of numArgs, and returns a message if numArgs is less than minArgs or greater than maxArgs.

    Note:   nargoutchk(minargs,maxargs,numargs) is not recommended.

msgStruct = nargoutchk(minArgs,maxArgs,numArgs,'struct') returns a message structure.

    Note:   nargoutchk(minargs,maxargs,numargs,'struct') is not recommended.

Examples

collapse all

Verify that a function is called with a minimum of two and maximum of five output arguments.

In a file, checkOutputs.m, in your current working folder, create the following function. The function signature indicates that checkOutputs accepts a variable number of output arguments.

function varargout = checkOutputs(varargin)
minArgs=2;  
maxArgs=5;
nargoutchk(minArgs,maxArgs)

disp("You requested " + nargout + " outputs.")

varargout = cell(nargout,1);
for k=1:nargout
    varargout{k} = randi(100);
end
end

At the command prompt, call the function with one output argument.

a = checkOutputs(13)
Error using checkOutputs (line 4)
Not enough output arguments.

Call the function again with five output arguments.

[a,b,c,d,e] = checkOutputs(7,42);
You requested 5 outputs.

Call the function again with six output arguments.

[a,b,c,d,e,f] = checkOutputs(7,42);
Error using checkOutputs (line 4)
Too many output arguments.

Input Arguments

collapse all

Minimum number accepted of outputs, specified as a scalar numeric value.

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

Maximum number accepted of outputs, specified as a scalar numeric value.

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

    Note:   Use of numargs is not recommended.

Number of function outputs, specified as a scalar numeric value. Typically, you use the nargout function to determine the number of output arguments specified in the function call.

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

Output Arguments

collapse all

    Note:   Use of msgText is not recommended.

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

If numArgs is less than minArgs, nargoutchk returns a character vector with the value 'Not enough output arguments.'. If numArgs is greater than maxArgs, nargoutchk returns a character value with the value 'Too many output arguments.'. Otherwise, nargoutchk returns an empty matrix.

    Note:   Use of msgStruct is not recommended.

Message and identifier, returned as a structure with message and identifier fields. If numArgs is less than minArgs, nargoutchk returns the following structure.

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

If numArgs is greater than maxArgs, nargoutchk returns the following structure.

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

Otherwise, nargoutchk returns an empty structure.

Tips

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

  • To verify that there is an exact number of arguments, specify the same value for minArgs and maxArgs For example: nargoutchk(3,3) throws an error when there are not exactly three outputs.

  • If minArgs is 0 and maxArgs is nargout, you do not need to use nargoutchk.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced before R2006a

Was this topic helpful?