| MATLAB Function Reference | ![]() |
Validate number of input arguments
Syntax
msgstring=nargchk(minargs, maxargs, numargs) msgstring=nargchk(minargs, maxargs, numargs, 'string') msgstruct = nargchk(minargs, maxargs, numargs, 'struct')
Description
Use nargchk inside an M-file function to check that the desired number of input arguments is specified in the call to that function.
msgstring returns an error message string = nargchk(minargs, maxargs, numargs)
msgstring if the number of inputs specified in the call numargs is less than minargs or greater than maxargs. If numargs is between minargs and maxargs (inclusive), nargchk returns an empty matrix.
It is common to use the nargin function to determine the number of input arguments specified in the call.
msgstring is essentially the same as the command shown above, as = nargchk(minargs, maxargs, numargs, 'string')
nargchk returns a string by default.
msgstruct returns an error message structure = nargchk(minargs, maxargs, numargs, 'struct')
msgstruct instead of a string. The fields of the return structure contain the error message string and a message identifier. If numargs is between minargs and maxargs (inclusive), nargchk returns an empty structure.
When too few inputs are supplied, the message string and identifier are
When too many inputs are supplied, the message string and identifier are
Remarks
nargchk is often used together with the error function. The error function accepts either type of return value from nargchk: a message string or message structure. For example, this command provides the error function with a message string and identifier regarding which error was caught:
If nargchk detects no error, it returns an empty string or structure. When nargchk is used with the error function, as shown here, this empty string or structure is passed as an input to error. When error receives an empty string or structure, it simply returns and no error is generated.
Examples
See Also
nargoutchk, nargin, nargout, varargin, varargout, error
| NaN | nargin, nargout | ![]() |
© 1994-2005 The MathWorks, Inc.