varargin
可变长度输入参数列表
语法
描述
varargin
是函数定义语句中的一个输入变量,允许函数接受任意数量的输入参数。使用小写字符指定 varargin
。在任何显式声明的输入项后,附加 varargin
作为最后一个输入参数。
在执行函数时,varargin
是一个 1×N 元胞数组,其中 N 是函数在显式声明的输入后收到的输入项数。如果该函数在显式声明的输入后未收到任何输入,则 varargin
是空元胞数组。
示例
个数不定的函数输入项
在名称为 acceptVariableNumInputs.m
的文件中定义一个函数,接受个数不定的输入项,并显示每个输入项的值。
type acceptVariableNumInputs
function acceptVariableNumInputs(varargin) disp("Number of input arguments: " + nargin) celldisp(varargin) end
使用多个输入调用该函数。
acceptVariableNumInputs(ones(3),'some text',pi)
Number of input arguments: 3 varargin{1} = 1 1 1 1 1 1 1 1 1 varargin{2} = some text varargin{3} = 3.1416
varargin
和声明的输入
在名为 definedAndVariableNumInputs.m
的文件中定义一个函数,它有两个必需的输入,并且接受一定数量的附加输入。
type definedAndVariableNumInputs
function definedAndVariableNumInputs(X,Y,varargin) disp("Total number of input arguments: " + nargin) formatSpec = "Size of varargin cell array: %dx%d"; str = compose(formatSpec,size(varargin)); disp(str) end
使用多个输入调用该函数。
definedAndVariableNumInputs(7,pi,rand(4),datetime('now'),'hello')
Total number of input arguments: 5 Size of varargin cell array: 1x3
使用两个输入调用该函数。varargin
是空元胞数组。
definedAndVariableNumInputs(13,42)
Total number of input arguments: 2 Size of varargin cell array: 0x0
可变数目的输入和输出
在名为 variableNumInputAndOutput.m
的文件中定义一个函数,它接受可变数目的输入和输出。
type variableNumInputAndOutput
function varargout = variableNumInputAndOutput(varargin) disp(['Number of provided inputs: ' num2str(length(varargin))]) disp(['Number of requested outputs: ' num2str(nargout)]) for k = 1:nargout varargout{k} = k; end end
使用两个输入和三个输出调用该函数。
[d,g,p] = variableNumInputAndOutput(6,'Nexus')
Number of provided inputs: 2 Number of requested outputs: 3
d = 1
g = 2
p = 3
不使用任何输入和输出再次调用该函数。
variableNumInputAndOutput
Number of provided inputs: 0 Number of requested outputs: 0
创建函数包装器
在工作文件夹中的某个文件中,为绘制红线的 plot 函数创建包装器。redplot
函数接受可变长度的输入参数列表,并返回可变长度的输出参数列表。它将线条颜色设置为红色,并将其他输入值转发给 plot
函数。此函数包装器允许您向 redplot
传递与 plot
相同的输入,而不用将线条颜色指定为红色。
type redplot.m
function varargout = redplot(varargin) [varargout{1:nargout}] = plot(varargin{:},'Color',[1,0,0]); end
使用 redplot
创建一个线图。
x = 0:pi/100:2*pi; y = sin(x); redplot(x,y)
再次调用 redplot
,并指定转发给 plot
函数的输入参数和输出参数。
h = redplot(x,y,'Marker','o','MarkerEdgeColor','green');
扩展功能
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
用法说明和限制:
如果您使用
varargin
定义入口(顶层)函数的参数,代码生成器会生成带固定数目输入参数的 C/C++ 函数。生成代码时指定的参数个数决定参数的固定数目。您无法写入
varargin
。如果要写入输入参数,请首先将输入参数复制到局部变量中。varargin
的索引必须为编译时常量。
HDL 代码生成
使用 HDL Coder™ 为 FPGA 和 ASIC 设计生成 Verilog 代码和 VHDL 代码。
用法说明和限制:
您无法写入
varargin
。如果要写入输入参数,请首先将输入参数复制到局部变量中。要在 HDL 模块属性 Architecture 设置为 “
MATLAB Datapath
” 的 MATLAB function 模块中使用varargin
,请注意以下事项:您只能通过使用
varargin
来创建元胞数组。不能使用其他初始化方法,例如使用cell
函数或使用花括号创建元胞数组,如cellArr = {1,2,3}
。您不能对元胞数组进行多重索引。例如,以下代码段会导致 HDL 代码生成出错。
[x, y] = varargin{1:2};
版本历史记录
在 R2006a 之前推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)