What the difference between using bracket and not.
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
0 Commenti
Risposta accettata
Stephen23
il 8 Mag 2020
Modificato: Stephen23
il 5 Giu 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.
0 Commenti
Più risposte (1)
Fangjun Jiang
il 8 Mag 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".
Vedere anche
Categorie
Scopri di più su Multidimensional Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!