nargchk
Validate number of input arguments.
📝Syntax
msg = nargchk(minArgs, maxArgs, n)
msg = nargchk(minArgs, maxArgs, n, 'string')
msgstruct = nargchk(minArgs, maxArgs, n, 'struct')
📥Input Arguments
Parameter Description
minArgs minimum number of accepted inputs (scalar integer value).
maxArgs maximum number of accepted inputs (scalar integer value).
n number of supplied inputs to check, usually nargin (scalar integer value).
'string' or 'struct' output type: 'string' (default) returns a character message, 'struct' returns an error structure.
📤Output Arguments
Parameter Description
msg a character row vector error message, or '' (empty) if n is in the range [minArgs, maxArgs].
msgstruct a structure with fields message and identifier (a 1x0 empty structure if n is in range).
📄Description

nargchk checks whether the number of input arguments n falls within the range [minArgs, maxArgs].

It returns the message 'Not enough input arguments.' when n is less than minArgs, 'Too many input arguments.' when n is greater than maxArgs, and an empty result otherwise.

It is typically used as error(nargchk(minArgs, maxArgs, nargin)) at the start of a function.

nargchk is deprecated and kept for compatibility with legacy code. Use narginchk instead in new code.

💡Examples
Not enough input arguments:
msg = nargchk(2, 3, 1)
Too many input arguments:
msg = nargchk(1, 2, 3)
In range returns an empty message:
msg = nargchk(1, 3, 2)
🔗See Also
narginchknargoutchknarginerror
🕔Version History
Version Description
2.0.0 initial version
Edit this page on GitHub