nargout 【转】
Matlab中nargout介绍
功能:在matlab中定义一个函数时,在函数体内部,nargout指出了输出参数的个数(nargin指出了输入参数的个数)。特别是在利用了可变参数列表的函数中,用nargout获取输入参数个数很方便。
语法:
nargout
nargout(fun)
相关:varargout、nargin、varargin
应用举例
function varargout = foo(varargin)
fprintf('How many output arguments? %d\n', nargout);
for k=1:nargout
varargout(k) = {varargin{k}};
end
end
输出结果
>> ret = foo(1, 2, 3)
How many output arguments? 1
ret = 1
>> [retA, retB] = foo(1, 2, 3)
How many output arguments? 2
retA = 1
retB = 2
>> [retA, retB, retC] = foo(1, 2, 3)
How many output arguments? 3
retA = 1
retB = 2
retC = 3