Main Content

nargout

Number of function output arguments

描述

例子

nargout返回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(乐趣返回出现在乐趣乐趣ction definition. If the function includesvarargout在其定义中nargout返回输出数量的负数。例如,如果功能myfun声明输出y,,,,z,,,,andvarargout, 然后nargout('myfun')返回-3

例子

全部收缩

在名称的文件中减去,创建一个计算第二个返回值的函数,absdif,仅在要求时。

类型减去
函数[dif,absdif] =减去(y,x)dif = y-x;如果nargout> 1 disp('计算绝对值')absdif = abs(dif);结尾

在命令提示下,致电减去具有一个返回值的功能。

diff = subtract(42,13)
diff = 29

致电减去乐趣ction again with two return values.

[DIF,ABSDIF] =减去(2,5)
计算绝对值
dif = -3
ABSDIF = 3

确定函数可以返回多少输出。

The function减去created in the previous example has two outputs in its declaration statement (difandabsdif)。

fun = @subtract;nargout(娱乐)
ANS = 2

确定使用多少输出的功能varargoutcan return.

在名称的文件中mysize。m,,,,create a function that returns a vector of dimensions from the尺寸功能和个人维度varargout

类型mysize。m
函数[sizevector,varargout] = mySize(x)sizevector = size(x);varargout =单元格(1,nargout-1);对于k = 1:长度(varargout)varargout {k} = sizevector(k);结尾

查询多少输出mysizecan return.

有趣=“ mysize';nargout(娱乐)
ANS = -2

The minus sign indicates that the second output isvarargout。Themysize功能可以返回不确定的其他输出数量。

输入参数

全部收缩

功能nargout从其定义中返回输出参数的数量,指定为函数句柄,字符向量或字符串标量。

例子:@rand

例子:'sortrows'

数据类型:char|function_handle

提示

  • 当您将函数用作表达式的一部分时,例如如果声明,然后是matlab®用一个输出参数调用功能。因此,nargout功能返回1在表达式中。

  • 如果您检查一个nargout函数中的值为0,您指定输出的值,MATLAB填充ans。但是,如果您检查nargout并且不指定输出的值,然后MATLAB不修改ans

扩展功能

版本历史记录

Introduced before R2006a