Effacer les filtres
Effacer les filtres

how can I insert comments into a multiline array creation?

6 vues (au cours des 30 derniers jours)
Andrew Diamond
Andrew Diamond le 27 Déc 2017
Commenté : Fergil Mills le 15 Juil 2019
This works
>> p = [...
%%%BEGIN ENTRIES %%%
'a1;', ...
'b;', ...
'c;'
]
p =
a1;b;c;
but commenting out the 'b' line doesn't
p = [...
%%%BEGIN ENTRIES %%%
'a1;', ...
% 'b;', ...
'c;'
]
Dimensions of matrices being concatenated are not consistent.

Réponse acceptée

Walter Roberson
Walter Roberson le 27 Déc 2017
p = [...
%%%BEGIN ENTRIES %%%
'a1;', ...
... % 'b;', ...
'c;'
]
Note: ... itself acts as a comment operator for the rest of the line, so you could get away with
p = [...
%%%BEGIN ENTRIES %%%
'a1;', ...
... 'b;', ...
'c;'
]
However this would tend to confuse people as it is not common to use this and readers might not pick up on the fact the rest of the line was a comment.
  2 commentaires
Andrew Diamond
Andrew Diamond le 27 Déc 2017
Thanks so much. So, is the idea that like a % an continuation ellipses means "ignore the rest of the line"? But then why does that work and not %? Inquiring minds want to know.
Walter Roberson
Walter Roberson le 27 Déc 2017
... has the side effect of also ignoring the rest of the line, but it has the primary effect of continuing the previous statement.
The portion of the 'b;' line before the % is not commented out, so
p = ['a1;', ...
% 'b;', ...
'c;']
is the same as
p = ['a1;', ...
'c;']
which is not the same as
p = ['a1;', ...
'c;']
The
p = ['a1;', ...
'c;']
version should be looked at as if it were
p = ['a1;', ...
[]
'c;']
which would be the same as
p = ['a1;', []
'c;']
The default within [] is that lines that do not end in continuation are treated as if they had ; at the end of them, so this is
p = ['a1;', [];
'c;']
which is
p = ['a1;';
'c;']
and that fails because the number of columns is not equal.

Connectez-vous pour commenter.

Plus de réponses (1)

Andrew Diamond
Andrew Diamond le 27 Déc 2017
I've been doing matlab for about 30 years and I didn't know this (the implicit empty array row insertion as well as the continuing ellipses at the start of a line as a continuation of the previous line etc). You learn something new every day.
again, thanks so much.
  1 commentaire
Fergil Mills
Fergil Mills le 15 Juil 2019
^ Agree w Andrew, thanks so much for posting this answer!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by