MATLAB Answers

Line comment change string cell shape

6 views (last 30 days)
JackXu on 20 Dec 2019
Commented: JackXu on 20 Dec 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 =
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.
JackXu on 20 Dec 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
I only find this to tell me I just can use (...) for multi lines
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.

Sign in to comment.

Accepted Answer

Chien-Han Su
Chien-Han Su on 20 Dec 2019
Edited: Chien-Han Su on 20 Dec 2019
I just try a two different line comment (matlab2019b), and here are the results,
>> a = {'A', 'B'}
a =
1×2 cell array
{'A'} {'B'}
>> a = {'A'
a =
2×1 cell array
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'}
, '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'}
JackXu on 20 Dec 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
is much clear. Also,
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!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by