| MATLAB Function Reference | ![]() |
Syntax
Description
In the body of a function M-file, nargin and nargout indicate how many input or output arguments, respectively, a user has supplied. Outside the body of a function M-file, nargin and nargout indicate the number of input or output arguments, respectively, for a given function. The number of arguments is negative if the function has a variable number of arguments.
nargin
returns the number of input arguments specified for a function.
nargin(' returns the number of declared inputs for the M-file function fun')
fun or -1 if the function has a variable of input arguments.
returns the number of output arguments specified for a function.nargout
nargout(' returns the number of declared outputs for the M-file function fun')
fun.
Examples
This example shows portions of the code for a function called myplot, which accepts an optional number of input and output arguments:
function[x0,y0]=myplot(fname,lims,npts,angl,subdiv) % MYPLOTPlotafunction. %MYPLOT(fname,lims,npts,angl,subdiv) %Thefirsttwoinputargumentsare %required;theotherthreehavedefaultvalues.... ifnargin<5,subdiv=20;end ifnargin<4,angl=10;end ifnargin<3,npts=25;end... ifnargout==0plot(x,y) elsex0=x;y0=y; end
See Also
inputname, varargin, varargout, nargchk, nargoutchk
| nargchk | nargoutchk | ![]() |