Documentation

This is machine translation

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

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country 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 in the call to the currently executing function. narginchk throws an error if the number of inputs specified in the call is fewer than minArgs or greater than maxArgs. If the number of inputs is between minArgs and maxArgs (inclusive), then 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 named checkInputs.m, create a function that uses narginchk to verify that the function has been called with a valid number of inputs. The function signature indicates that checkInputs requires two input arguments and accepts up to three additional, optional arguments.

function checkInputs(A,B,varargin)
    minArgs=2;
    maxArgs=5;
    narginchk(minArgs,maxArgs)
    
    fprintf('Received 2 required and %d optional inputs\n', length(varargin))
end

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

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

Maximum number of accepted inputs, specified as a scalar.

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

Tips

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

  • To verify that you have an exact number of arguments, specify the same value for minArgs and maxArgs. For example: narginchk(3,3) throws an error if you do not have 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), then 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?