varargin
Variable-length input argument list
Syntax
varargin
Description
varargin
is an input variable in a function definition statement that enables the function to accept any number of input arguments. Specifyvarargin
using lowercase characters, and include it as the last input argument after any explicitly declared inputs.
When the function executes,varargin
is a 1-by-Ncell array, whereNis the number of inputs that the function receives after the explicitly declared inputs. However, if the function receives no inputs after the explicitly declared inputs, thenvarargin
is an empty cell array.
Examples
Variable Number of Function Inputs
定义一个函数在文件命名acceptVariableNumInputs.m
that accepts a variable number of inputs and displays the values of each input.
typeacceptVariableNumInputs
function acceptVariableNumInputs(varargin) disp("Number of input arguments: " + nargin) celldisp(varargin) end
Call the function with several inputs.
acceptVariableNumInputs(ones(3),'some text',pi)
Number of input arguments: 3 ans{1} = 1 1 1 1 1 1 1 1 1 ans{2} = some text ans{3} = 3.1416
varargin
and Declared Inputs
定义一个函数在文件命名definedAndVariableNumInputs.m
that expects two inputs and accepts an additional number of inputs.
typedefinedAndVariableNumInputs
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
Call the function with several inputs.
definedAndVariableNumInputs(7,pi,rand(4),datetime('now'),'hello')
Total number of input arguments: 5 Size of varargin cell array: 1x3
Call the function with two inputs.varargin
is an empty cell array.
definedAndVariableNumInputs(13,42)
Total number of input arguments: 2 Size of varargin cell array: 0x0
Variable Number of Inputs and Outputs
定义一个函数在文件命名variableNumInputAndOutput.m
that accepts a variable number of inputs and outputs.
typevariableNumInputAndOutput
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
Call the function with two inputs and three outputs.
[d,g,p] = variableNumInputAndOutput(6,'Nexus')
Number of provided inputs: 2 Number of requested outputs: 3
d = 1
g = 2
p = 3
Call the function again with no inputs or outputs.
variableNumInputAndOutput
Number of provided inputs: 0 Number of requested outputs: 0
Create Function Wrapper
In a file in your working folder, create a wrapper to the plot function that plots a red line. Theredplot
function accepts a variable-length input argument list and returns a variable-length output argument list. It sets the line color to red, and forwards other input values to theplot
函数。这个函数包装器允许您不是sredplot
the same inputs asplot
and not specify that the line color is red.
typeredplot.m
function varargout = redplot(varargin) [varargout{1:nargout}] = plot(varargin{:},'Color',[1,0,0]); end
Useredplot
to create a line plot.
x = 0:pi/100:2*pi; y = sin(x); redplot(x,y)
Callredplot
again, and specify input and output arguments to forward to theplot
函数。
h = redplot(x,y,'Marker','o','MarkerEdgeColor','green');
See Also
介绍了在e R2006a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
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)