Why does a commented out line generate an error

8 vues (au cours des 30 derniers jours)
Leo Simon
Leo Simon le 8 Juin 2018
Commenté : Walter Roberson le 8 Juin 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.

Réponse acceptée

Walter Roberson
Walter Roberson le 8 Juin 2018
Modifié(e) : Walter Roberson le 8 Juin 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 commentaires
Leo Simon
Leo Simon le 8 Juin 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 le 8 Juin 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."

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by