What the difference between using bracket and not.

3 次查看(过去 30 天)
Hi I'm newbie of matlab.
function varargout = redplot(varargin)
[varargout{1:nargout}] = plot(varargin{:},'Color',[1,0,0]);
end
this code doesn't appear the error.
but
function varargout = redplot(varargin)
varargout{1:nargout} = plot(varargin{:},'Color',[1,0,0]);
end
this code shows the error.
i don't know why the last cod appears the error.
the difference between them is [ ].
what is the role [ ] in this code.
Thank you.

采纳的回答

Stephen23
Stephen23 2020-5-8
编辑:Stephen23 2022-6-5
None of the answers explain why this works, nor even mentioned the useful-to-know name of this syntax.
The actual reason is because that syntax combines two different syntaxes together. These are:
1- multiple function outputs are always indicated by square brackets, the outputs are written in the form of a comma-separated list, e.g.:
[a,b,c,d] = somefun();
This is explained in the introductory tutorials:
2- one cell array can be converted to (or from) a comma-separated list using this syntax:
somecell{:}
which is equivalent to this comma-separated list:
somecell{1},somecell{2},somecell{3},...
Read more about how comma-separated lists work:
Combine these two different syntaxes into one, to allocate multiple function outputs into one cell array:
[somecell{:}] = somefun();
Add indexing as required.
PS: the syntax somecell{:}=somefun() is meaningless.

更多回答(1 个)

Fangjun Jiang
Fangjun Jiang 2020-5-8
I think the fundenmental reason is that the function definition requires that the left side contains "[ ]" when there are multiple outputs. See "doc function".

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by