필터 지우기
필터 지우기

What the difference between using bracket and not.

조회 수: 2 (최근 30일)
Junu Lee
Junu Lee 2020년 5월 8일
편집: Stephen23 2022년 6월 5일
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에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by