Why does a commented out line generate an error

5 Ansichten (letzte 30 Tage)
Leo Simon
Leo Simon am 8 Jun. 2018
Kommentiert: Walter Roberson am 8 Jun. 2018
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 8 Jun. 2018
Bearbeitet: Walter Roberson am 8 Jun. 2018
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 Kommentare
Leo Simon
Leo Simon am 8 Jun. 2018
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 am 8 Jun. 2018
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."

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by