nargchk
Old-style check of the number of input arguments; superseded by narginchk.
Category: Programming > Functions
Syntax
msg = nargchk(minargs, maxargs, numargs)
Returns an error message if numargs lies outside minargs to maxargs, otherwise an empty value.
msg = nargchk(minargs, maxargs, numargs, 'struct')
Returns the problem as a structure with message and identifier fields.
Description
nargchk only builds a message; it does not raise an error. The caller must test the result and call error. New code should use narginchk, which throws the error directly with the standard text.
Input arguments
minargs- Smallest acceptable count.
maxargs- Largest acceptable count;
Infmeans no limit. numargs- The actual count, normally
nargin.
Output arguments
msg- Error message text, or an empty value when the count is acceptable.
Example
Calling it inside a function
function r = scale(x, k)
nargchk(1, 2, nargin);
if nargin < 2
k = 10;
end
r = x * k;
end
disp(scale(4))
disp(scale(4, 3))
Output
40
12
Notes
In MatLivre nargchk is accepted but performs no check, so wrong argument counts are not reported. Use error yourself if a hard failure is needed.