Why does a commented out line generate an error

7 ビュー (過去 30 日間)
Leo Simon
Leo Simon 2018 年 6 月 8 日
コメント済み: Walter Roberson 2018 年 6 月 8 日
The following code throws an error
CellArray = { ...
'X' ...
% ,'X' ...
,'X' ...
};
The error is:
Dimensions of matrices being concatenated are not consistent.
If I erase the commented out line, there is no error. So my question is: why isn't commenting out a line equivalent to erasing it.

採用された回答

Walter Roberson
Walter Roberson 2018 年 6 月 8 日
編集済み: Walter Roberson 2018 年 6 月 8 日
Using ... is equivalent to bringing the next line up to the end of the current line. So that code is equivalent to
CellArray = { 'X' % ,'X' ...
,'X' };
The last last of those lines is not brought up to the previous one because the ... itself on the line before it has been commented out.
You need to use
CellArray = { ...
'X' ...
... % ,'X' ...
,'X' ...
};
Or, since ... itself acts to comment out the rest of the line,
CellArray = { ...
'X' ...
... ,'X' ...
,'X' ...
};
  2 件のコメント
Leo Simon
Leo Simon 2018 年 6 月 8 日
Thanks Walter. Could you explain why ... comments out the rest of the line? I thought it was just a way of continuing two lines
Walter Roberson
Walter Roberson 2018 年 6 月 8 日
It is by definition.
"Three or more periods at the end of a line continues the current command on the next line. If three or more periods occur before the end of a line, then MATLAB ignores the rest of the line and continues to the next line. This effectively makes a comment out of anything on the current line that follows the three periods."

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

その他の回答 (0 件)

カテゴリ

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