Effacer les filtres
Effacer les filtres

What the difference between using bracket and not.

2 vues (au cours des 30 derniers jours)
Junu Lee
Junu Lee le 8 Mai 2020
Modifié(e) : Stephen23 le 5 Juin 2022
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.

Réponse acceptée

Stephen23
Stephen23 le 8 Mai 2020
Modifié(e) : Stephen23 le 5 Juin 2022
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.

Plus de réponses (1)

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

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by