How to prevent cell array to transpose cell automatically?

Hi everyone,
I am encountering a problem when running my script. I am running a for loop and storing the data into a cell array as the length changes with every loop. An example of the result is shown below:
14x1 cell
11x1 cell
1x11 cell
14x1 cell
1x14 cell
When the string starts with a zero it automatically transposes from 11x1 to 1x11. And I do not know from upfront which cell will start with a zero. I want to write it to excel into different columns, which works as long as it does not start with zero and therefore does not transpose.
How can I prevent this?
Thanks in advance!

 Accepted Answer

"When the string starts with a zero it automatically transposes" That is really not clear since we have no idea what "string" you're talking about. "it" is the code you wrote, so the problem is with your code or your understanding of the way matlab works. For us to help you there, we'd need to see the portion of code that does not work the way you want.
Otherwise, you can always fix it afterward with:
%faultycellarray: your cell array with a mix of column and row vectors. Replace name as appropriate
newcellarray = cellfun(@(c) c(:), faultycellarray, 'UniformOutput', false); %reshape each cells as column vectors
Of course, it would be better to fix the bug in your code.

1 Comment

It works! Thank you! I am still a novice in Matlab, so I will have to work on my understanding. :)

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!