acessing cell arrays with different number of rows

Hi,
I have cell array "out" 1x123 cells. Within each cell of out{1,1}, <1x3 cells. How can I access 2 and 3 columns of each cell within "out" and store in flows2 matrix. I am having a difficulty because each cell of "out" has different number of rows.
So, I tried this way
for j=1:length(csvFiles)
formatOut = 'yyyy';
flows2(:,j)=[str2num(datestr(datenum([out{1,j}{1,2}],1900),formatOut)) [out{1,j}{1,3}]];
end
I am receiving this error. Subscripted assignment dimension mismatch.
My question is, any other efficient way of doing this?
Any suggestion is appreciated.
Thanks in advance,

 Accepted Answer

The only option I can see is to create flows2 as a cell array:
flows2{j} = ...

6 Comments

Now, next question is how can I access each cell of flows2 2nd column?
for example to acess the cell 1 of flows2 and 2nd column of cell 1.
flows2{1,1},2 or?
To get the contents of flows2{1}, use the curly-bracket notation:
% Create data:
for j = 1:4
flows2{j} = [randi(10,j+2,1)+2000 randi(10,j+2,1)];
end
% Getting the contents of ‘flows2{1}’:
q = flows2{1}
produces:
q =
2009 9
2010 4
2008 8
The ‘q’ variable here is now a (3x2) double array you can address as you would any other double array. (You cannot use cell2mat for flows2 because it has uneven columns.)
I understand. But is there a way to read the columns of each cell.? for example column 2 of each cell 1 and store is separate array.
I apologise for the misunderstanding.
To get the second column of flows2{1}:
q1 = flows2{1}
q2 = flows2{1}(:,2)
Here, ‘q1’ is the previous ‘q’ (to check the result), and ‘q2’ is the second column of flows2{1}.
Thanks. How can I store q2 values in different columns for all the cells. Something like this,
for i=1:length(csvFiles)
q2(:,i)=flows2{i}(:,2)
end
When I tried this I received this error: Subscripted assignment dimension mismatch.
I know know this is due to uneven number of rows of 2nd column of each cells. Is there a way to store with different names like q1,q2,....length(csvFiles)?
For the same reason as with flows2 (different size columns), you will need to store them in a cell array:
for i=1:length(csvFiles)
q2{i}=flows2{i}(:,2)
end
If you wanted to, you could do that in your ‘j’ loop. Simply add it as a separate line and save an extra loop.

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!