Why does a commented out line generate an error

5 visualizaciones (últimos 30 días)
Leo Simon
Leo Simon el 8 de Jun. de 2018
Comentada: Walter Roberson el 8 de Jun. de 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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 8 de Jun. de 2018
Editada: Walter Roberson el 8 de Jun. de 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 comentarios
Leo Simon
Leo Simon el 8 de Jun. de 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 el 8 de Jun. de 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."

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by