MATLAB Answers

What does the varargin function do and what does varargin{:} mean?

11 views (last 30 days)
Victoria Helm
Victoria Helm on 24 Jun 2020
Edited: Tommy on 26 Jun 2020
For example: [varargin{:}] = convertStringsToChars(varargin{:});

  3 Comments

Tommy
Tommy on 24 Jun 2020
varargin{:} creates a comma-separated list. On the right hand side of your example, the syntax is being used to create function call arguments. On the left hand side, it is being used to assign function return values (not concatenation, thanks Stephen!)
Stephen Cobeldick
Stephen Cobeldick on 24 Jun 2020
"On the left hand side, it is being used for concatenation."
This is incorrect: no arrays are being concatenated.
When used on the LHS it refers to the elements of the cell array, just like on the RHS. The only difference is that values are being assigned to those elements rather than extracted from them. This is explained in the section "Assigning to a Comma-Separated List" here:
Tommy
Tommy on 26 Jun 2020
Somehow I missed this comment until now. Yikes, thank you for pointing out the mistake. Hopefully it is now correct.

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 24 Jun 2020
Edited: Stephen Cobeldick on 24 Jun 2020
"What does the varargin function do..."
varargin is not a function, it is a cell array which contains any number of optional input arguments:
"... and what does varargin{:} mean?"
That syntax creates a comma-separated list from the cell array varargin:
So your example is equivalent to this:
[varargin{1},varargin{2},...,varargin{end}] = convertStringsToChars(varargin{1},varargin{2},...,varargin{end});

  0 Comments

Sign in to comment.

More Answers (1)

KSSV
KSSV on 24 Jun 2020
varargin stands for variable number of arguments. You can input any number of arguments to the function. And these inputs are read by nargin which means the number of inputs.

  0 Comments

Sign in to comment.


Translated by