Main Content

nargoutchk

Validate number of output arguments

Description

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

example

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

msgStruct = nargoutchk(minArgs,maxArgs,numArgs,'struct') returns a message structure. The use of this syntax 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 named checkOutputs.m, create a function that uses nargoutchk to verify that the function has been called with a valid number of outputs. 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

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 of accepted outputs, specified as a scalar.

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

Maximum number of accepted outputs, specified as a scalar.

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. 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, then nargoutchk returns the character vector 'Not enough output arguments.' If numArgs is greater than maxArgs, then nargoutchk returns the character vector '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 this structure:

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

If numArgs is greater than maxArgs, nargoutchk returns this structure:

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

Otherwise, nargoutchk returns an empty structure.

Tips

  • To verify that you have a minimum number of arguments, but no maximum number, set maxArgs to inf. For example: nargoutchk(5,inf) throws an error when there are fewer than five outputs.

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

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

Extended Capabilities

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

Version History

Introduced before R2006a