Line comment change string cell shape

2 visualizzazioni (ultimi 30 giorni)
JackXu
JackXu il 20 Dic 2019
Commentato: JackXu il 20 Dic 2019
I found when I use line comment to erase some my content for a string cell, the cell shape change to row from original column
like this
>> a={'first' ...
, 'second' ...
, 'third'}
a =
'first' 'second' 'third'
>> a={'first' ...
% , 'second' ...
, 'third'}
a =
'first'
'third'
I have already tried 2013a and 2015b, they shared the same result.
I just want to change my test case sometime quickly for a simple test, but that messed up the following code with my for loop will use like this
for a_n = a
a_n = a_n{:};
That will be good if I don't comment anything, but failed to only loop once with my first case.
Any answer about why the shape of string cell will change when there are line comments is appreciated.
  2 Commenti
Bhaskar R
Bhaskar R il 20 Dic 2019
I hope I understood your problem.
You have a cell matrix a
>> a={'first' ...
, 'second' ...
, 'third'}
a is a row matrix(shape is 1x3), if you comment any row/line of the a matrix, the shape of the matrix is converted to column matrix( that is 2x1 instread of 1x2), am I correct? if yes
you have used
...
in your matrix initialization that means continuous line that is equivalent to
a = {'first' , 'second','third'};
it is suggested you that remove "..." from the initialization so that you can preserve shape of the matrix a
JackXu
JackXu il 20 Dic 2019
Thanks a lot, you understood my question.
I really meant to ask that can I keep my matrix shape with this kind of matrix initalization.
I want to line up my operation case into every single line, so that I can just use line comment (ctrl + R) or line uncomment (ctrl + T) quickly to change my operation content and run another turn easily with F5.
I can't easily organize my initialization into just one line, because the string I use usually is about 8 in length, and I get 10 case string. If they are all into one line, that would be horrible to find and change anything like that.
Furthermore, I already find another bad news for this kind of writing method. If I don't comment second line but comment the third line, only error I can get. So comment different line also has different result?
>> a = {'first' ...
, 'second' ...
% , 'third' ...
, 'four' ...
}
Dimensions of matrices being concatenated are not consistent.
So, how this
...
works?
I only find this to tell me I just can use (...) for multi lines
https://ww2.mathworks.cn/help/matlab/matlab_prog/continue-long-statements-on-multiple-lines.html?lang=en
Now, it seems that line comment will change the effect of ellipsis. I want to know more about both of them, so I may find other way to change my code. Of course, if you could share your method to organize plenty of operation case, that would be really really helpful.
Always, thanks a lot for your help. And all help is appreciated.

Accedi per commentare.

Risposta accettata

Chien-Han Su
Chien-Han Su il 20 Dic 2019
Modificato: Chien-Han Su il 20 Dic 2019
I just try a two different line comment (matlab2019b), and here are the results,
(1)
>> a = {'A', 'B'}
a =
1×2 cell array
{'A'} {'B'}
(2)
>> a = {'A'
,'B'}
a =
2×1 cell array
{'A'}
{'B'}
You can see that matlab sees the second assignment equal to
a = {'A';'B'}
So a column cell array is assigned.
In your example,
a={'first' ...
% , 'second' ...
, 'third'}
equals to
a={'first' ... % , 'second' ...
, 'third'}
and
a={'first'
, 'third'}
Hence you get a column like the previous (2).
If you want to erase some content and keep the row shape at the same time, maybe you should also add '...' before the '%', namely,
a={'first' ...
... % , 'second' ...
, 'third'}
gives you
a =
1×2 cell array
{'first'} {'third'}
  3 Commenti
Chien-Han Su
Chien-Han Su il 20 Dic 2019
I just come up with an idea to maintain your ways to switch on and off contents in the cell with the comment hotkey, you can try store your cell as column at first, then transpose it into a row befroe executing the loop, that is, replace
a={'first' ...
, 'second' ...
, 'third'}
with
a={'first' ...
; 'second' ...
; 'third'}
and before executing the loop, force the cell in the shape of row
a = a(:) % force 'a' into a column
% (This line above can be dropped if you are sure that 'a' is a column)
a = a'; % transpose 'a' into a row
for a_n = a
a_n = a_n{:};
end
JackXu
JackXu il 20 Dic 2019
Awesome! We made it in the same way.
I already changed my code like this
>> a = {'first' ...
; 'second' ...
% ; 'third' ...
; 'fourth' ...
};
a = reshape(a, 1, numel(a))
a =
'first' 'second' 'fourth'
And now it alrealy works fine and it is totally my style.
I barely use
a = a(:)
because I always misunderstand what this
(:)
will give me, and
reshape
is much clear. Also,
a'
is clear but context-sensitive, and I just use it when I need transpose.
It just like the symbol we use in linear algebra.
Always, you are great helpful!

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by