Skip to content

nargchk

Validate the number of input arguments.

msg = nargchk(nminarg, nmaxarg, numargs)
msg = nargchk(nminarg, nmaxarg, numargs, 'string')

Returns an error message string if numargs is outside [nminarg, nmaxarg], otherwise returns an empty string. The 'string' flag controls return format. Used inside function bodies to validate argument count before processing.

Note: superseded by direct comparisons (if nargin < N || nargin > M) in modern code.

function foo(a, b, c)
msg = nargchk(2, 3, nargin); error(msg);
end
  • nargoutchk — Validate the number of output arguments.
  • nargin — Number of input arguments passed to the current function.
  • nargout — Number of output arguments requested by the caller.