nargoutchk - Validate number of output arguments (2024)

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

minArgsMinimum 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

maxArgsMaximum 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

numArgsNumber 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 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

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.

nargoutchk - Validate number of output arguments (1)

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)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

nargoutchk - Validate number of output arguments (2024)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Jamar Nader

Last Updated:

Views: 5706

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Jamar Nader

Birthday: 1995-02-28

Address: Apt. 536 6162 Reichel Greens, Port Zackaryside, CT 22682-9804

Phone: +9958384818317

Job: IT Representative

Hobby: Scrapbooking, Hiking, Hunting, Kite flying, Blacksmithing, Video gaming, Foraging

Introduction: My name is Jamar Nader, I am a fine, shiny, colorful, bright, nice, perfect, curious person who loves writing and wants to share my knowledge and understanding with you.