Number of function output arguments
nargout returns the number of function output arguments
specified in the call to the currently executing function. Use this syntax in the
body of a function only.
nargout( returns the number of
outputs that appear in the fun)fun function definition. If the
function includes varargout in its definition, then
nargout returns the negative of the number of outputs. For
example, if function myFun declares outputs y,
z, and varargout, then
nargout('myFun') returns -3.
When you use a function as part of an expression, such as an if statement, then MATLAB® calls the function with one output argument. Therefore, the nargout function returns 1 within expressions.
If you check for a nargout value of 0 within a function and you specify the value of the output, MATLAB populates ans. However, if you check nargout and do not specify a value for the output, then MATLAB does not modify ans.