フィルターのクリア

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".
  1 件のコメント
Junu Lee
Junu Lee 2020 年 5 月 8 日
thanks a lot Fangjun!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by