Validate number of output arguments
collapse all in page
Syntax
nargoutchk(minArgs,maxArgs)
msgText = nargoutchk(minArgs,maxArgs,numArgs)
msgStruct = nargoutchk(minArgs,maxArgs,numArgs,'struct')
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
Number of Outputs Within Expected Range
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);endend
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
minArgs
— Minimum number of accepted outputs
scalar
Minimum number of accepted outputs, specified as a scalar.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
maxArgs
— Maximum number of accepted outputs
scalar
Maximum number of accepted outputs, specified as a scalar.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
numArgs
— Number of function outputs
scalar
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
msgText
— Message text
'Not enough output arguments.'
| 'Too many output arguments.'
| empty matrix
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.
msgStruct
— Message and identifier
structure
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
toinf
. 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
andmaxArgs
. For example:nargoutchk(3,3)
throws an error when you do not have exactly three outputs.If
minArgs
is 0 andmaxArgs
isnargout
, then you do not need to usenargoutchk
.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced before R2006a
See Also
nargout | varargout
Topics
- Validate Number of Function Arguments
MATLAB-Befehl
Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:
Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office