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

222 次查看(过去 30 天)
For example: [varargin{:}] = convertStringsToChars(varargin{:});
  3 个评论
Stephen23
Stephen23 2020-6-24
"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 2020-6-26
编辑:Tommy 2020-6-26
Somehow I missed this comment until now. Yikes, thank you for pointing out the mistake. Hopefully it is now correct.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2020-6-24
编辑:Stephen23 2022-2-23
"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});

更多回答(1 个)

KSSV
KSSV 2020-6-24
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.

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by