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.

narginchk

Validate number of input arguments

Syntax

narginchk(minArgs,maxArgs)

Description

example

narginchk(minArgs,maxArgs) validates the number of input arguments. narginchk throws an error if the number of inputs specified in the call to the currently executing function is less than minArgs or greater than maxArgs. If the number of inputs is between minArgs and maxArgs (inclusive), narginchk does nothing.

Examples

collapse all

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

In a file, checkInputs.m, in your current working folder, create the following function. The function signature indicates that checkInputs requires two input arguments and accepts additional, optional arguments.

function checkInputs(A,B,varargin)
minArgs=2;  
maxArgs=5;
narginchk(minArgs,maxArgs)

disp("Received 2 required and " + length(varargin) + " optional inputs")
end

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

checkInputs(13)
Error using checkInputs (line 4)
Not enough input arguments.

Call the function again with five input arguments.

checkInputs(13,7,42,1701,5)
Received 2 required and 3 optional inputs

Call the function again with six input arguments.

checkInputs(13,7,42,1701,5,88)
Error using checkInputs (line 4)
Too many input arguments.

Input Arguments

collapse all

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

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

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

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

Tips

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

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

  • If you call a function with too few inputs, the message identifier and message are:

        identifier: 'MATLAB:narginchk:notEnoughInputs'
           message: 'Not enough input arguments.'
    

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

        identifier: 'MATLAB:narginchk:tooManyInputs'
           message: 'Too many input arguments.'
    

  • If minArgs is 0 and maxArgs is nargin(fun), you do not need to use narginchk.

Extended Capabilities

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

Introduced in R2011b

Was this topic helpful?