MATLAB Answers

varargin

908 views (last 30 days)
Ahmed Tawfeeq
Ahmed Tawfeeq on 14 Feb 2012
Edited: sixwwwwww on 12 Oct 2013
what is varargin used for ?and how to use it? and can I change it to an ordinary input variable?

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 14 Feb 2012
There are situations in which it is not possible to use any fixed number of arguments, so conversion depends upon what your code does.

  0 Comments

Sign in to comment.

More Answers (2)

Matt Tearle
Matt Tearle on 14 Feb 2012
It is used for situations where you want to allow any number of input arguments (so you can't list them). A common reason for that is optional settings -- in MATLAB this is often done with property name/property value pairs. For example, think of how you can pass extra options to plot:
plot(x,y) % plain
plot(x,y,'LineWidth',4)
plot(x,y,'Marker','o','LineWidth',2)
plot(x,y,'Marker','o','LineWidth',2,'MarkerFaceColor','r')
etc. Here you don't want to force the user to input all the possible optional parameters in a specific order, so instead you allow them to specify 'MagicPropertyName' takes the value PropertyValue (eg 'LineWidth' takes the value 2).
To write a function that can do this, you'd make a function declaration like
function plot(x,y,varargin)
Then inside this function, x takes the value of the first input, y takes the value of the second, and all the rest go into cells of the cell array varargin. So, in the last example, varargin would be a 1-by-6 cell array; varargin{1} would be the string 'Marker', varargin{4} would be the number 2, etc.
So obviously there's a bunch of programming you have to do as the developer to parse the elements of varargin, and make your code behave accordingly.
I don't really understand the last question. Could you elaborate?
(Note: the actual plot function is actually even more flexible than this -- the above function declaration line is for illustration only.)

  3 Comments

Ahmed Tawfeeq
Ahmed Tawfeeq on 14 Feb 2012
I have a simulink project and I encounter a problem in calling the function adaptfilt.lms to matlab function block because of two reasons first : I can't find a way to define the data type of this function and second: adaptfilt.lms obviously have a dot..one of mathworks contributors suggests to use the lms.m (contains varargin) inside the [C:\Program Files\MATLAB\R2011a\toolbox\dsp\filterdesign\@adaptfilt\@lms\lms.m % adaptfilt.lms constructor
] to get rid of the problem of the dot but there is not benefit because an error message appears says: varargin cannot be used in top level functions...(that once a run the simulink of the block contains the lms.m)...so I decided that i use an ordinary variable to get rid of this problem....
Walter Roberson
Walter Roberson on 14 Feb 2012
You cannot pass adaptfilt.lms objects around in Simulink . No efforts along the line of calling lms.m directly are going to work.
Walter Roberson
Walter Roberson on 14 Feb 2012
Alternatives discussed in one of your other Questions.

Sign in to comment.


Benjamin Schwabe
Benjamin Schwabe on 14 Feb 2012
varargin is used when the number of input parameter might change. Basically, it puts all input arguments into a cell array. The number of input parameters is given by
nargin.
You can access any of the input values inside the function by
varargin{ind}
where ind is the index number of your argument.

  0 Comments

Sign in to comment.

Tags