narginchk
Checks that a function was called with an acceptable number of input arguments.
Category: Programming > Functions
Syntax
narginchk(minargs, maxargs)
Errors if the calling function's nargin is below minargs or above maxargs.
Description
Call narginchk at the top of a function that accepts optional arguments through varargin or through early use of nargin. It reads nargin of the function that called it, so no count has to be passed. When the count is out of range the error is raised in the caller with the standard messages Not enough input arguments. or Too many input arguments.
Input arguments
minargs- Smallest acceptable number of inputs, a non-negative integer.
maxargs- Largest acceptable number of inputs;
Infmeans no upper limit.
Example
Optional second argument
function r = addn(x, n)
narginchk(1, 2);
if nargin < 2
n = 1;
end
r = x + n;
end
disp(addn(5))
disp(addn(5, 10))
Output
6
15
Notes
In MatLivre narginchk is accepted but performs no check, so wrong argument counts are not reported. Use error yourself if a hard failure is needed.