varargin
Variable-length input argument list
句法
描述
varargin
是函数定义语句中的输入变量,使该函数能够接受任何数量的输入参数。指定varargin
通过使用小写字符。明确声明输入后,包括varargin
作为最后一个输入参数。
当函数执行时,varargin
是1乘n细胞阵列,其中n是该函数在明确声明的输入后接收的输入数量。如果该函数在明确声明输入后未收到输入,则varargin
是一个空的单元格数组。
例子
函数输入数量可变
Define a function in a file namedAccessVariablenumInputs.m
that accepts a variable number of inputs and displays the values of each input.
类型Appactvariablenuminputs
函数AcceptVariablenumInputs(varargin)disp(“输入参数的数量:” + nargin)celldisp(varargin)结束
称呼the function with several inputs.
Appactvariablenuminputs(ones(3),'some text',,,,pi)
输入参数的数量:3 varargin {1} = 1 1 1 1 1 1 1 1 1 1 1 1 varargin {2} =某些文本varargin {3} = 3.1416
varargin
and Declared Inputs
Define a function in a file named定义和瓦里亚布林普特
这期望两个输入并接受额外数量的输入。
类型定义和瓦里亚布朗图斯
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
称呼the function with several inputs.
定义和variaiblenuminputs(7,Pi,Rand(4),DateTime('现在'),'你好')
输入参数的总数:5 varargin单元格数的大小:1x3
使用两个输入调用功能。varargin
是一个空的单元格数组。
定义和瓦里亚布朗图斯(13,42)
输入参数的总数:2 varargin单元格数组的大小:0x0
变量的输入和输出数量
Define a function in a file namedvariablenumInputandOutput.m
that accepts a variable number of inputs and outputs.
类型variableNumInputAndOutput
函数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
称呼the function with two inputs and three outputs.
[d,g,p] = variablenuminputandOutput(6,'关系')
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
创建功能包装器
In a file in your working folder, create a wrapper to the plot function that plots a red line. The红牌
函数接受可变长度输入参数列表,并返回一个可变长度的输出参数列表。它将行颜色设置为红色,并将其他输入值转发到阴谋
功能。此功能包装器使您可以通过红牌
输入与阴谋
并且不指定线色是红色的。
类型红牌。m
函数varargout = redplot(varargin)[varargout {1:nargout}] = plot(varargin {:},'color',[1,0,0]);结尾
Use红牌
to create a line plot.
x = 0:pi/100:2*pi;y = sin(x);redplot(x,y)
称呼红牌
再次,并指定输入和输出参数以转发到阴谋
功能。
h = redplot(x,y,'Marker',,,,'o',,,,'MarkerEdgeColor',,,,'绿色');
扩展功能
C/C ++代码生成
Generate C and C++ code using MATLAB® Coder™.
用法注释和限制:
如果您使用
varargin
为了将参数定义为入口点(顶部级)函数,代码生成器会产生具有固定数量的输入参数的C/C ++函数。生成代码时指定的参数数量确定固定数量的参数。You cannot write to
varargin
。If you want to write to input arguments, first copy the input arguments into a local variable.索引进入
varargin
必须是编译时常数。
HDL代码生成
Generate Verilog and VHDL code for FPGA and ASIC designs using HDL Coder™.
用法注释和限制:
You cannot write to
varargin
。If you want to write to input arguments, first copy the input arguments into a local variable.使用
varargin
在一个MATLABfunction具有HDL块属性的块建筑学调成MATLABDatapath
:You can create cell arrays through only the use of
varargin
。您不能使用其他初始化技术,例如使用细胞
功能或通过使用卷曲括号来创建单元格数组细胞Arr = {1,2,3}
。您不能将多指数置于单元格数组中。例如,此代码段导致HDL代码生成中的错误。
[x, y] = varargin{1:2};
版本历史记录
Abrir Ejemplo
Tiene unaversiónmodificada de esjemplo。„ Desea abrir este ejemplo con s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s ejemplo?
Comando de Matlab
Ha Hecho Clic en ener eNRACE que对应了este comando de matlab:
Ejecute El Comando介绍了comandos de Matlab。Los Navegadores Web no Anditen comandos de Matlab。
You can also select a web site from the following list:
如何获得最佳网站性能
选择中国网站(中文或英语)以获得最佳场地性能。其他Mathworks乡村网站未针对您所在的访问进行优化。
美洲
- América Latina(Español)
- 加拿大(英语)
- 美国(英语)