How to export a cell array, into excel, with elements of different sizes

I have a cell whose cells are vector of different sizes. As an example:
cell ={[50x1 double] [32x1 double] [288x1 double]}
How do I export this into an excel file, and in each column of the excel file goes a vector? I only could make with one element of the cell, because of the different sizes of the contents of the cell.
Thanks already

 Accepted Answer

You have to extract the arrays from each cell and put them into individual cells. So, for example with the first cell, you need to do something like
theArray = originalCellArray{1}; % Extract first cell.
ca = cell(288,3);
for row = 1 : size(theArray, 1)
ca{row,1} = theArray(row);
end
Do that for each of your 3 cells. You can have them all in one cell.
theArray = originalCellArray{2}; % Extract second cell.
for row = 1 : size(theArray, 1)
ca{row,2} = theArray(row);
end
theArray = originalCellArray{3}; % Extract third cell.
for row = 1 : size(theArray, 1)
ca{row,3} = theArray(row);
end
xlswrite(filename, ca);
You might want to read this before you start (in case I programmed something up incorrectly - I didn't test it): http://matlab.wikia.com/wiki/FAQ?title=FAQ&cb=8838#What_is_a_cell_array.3F

5 Comments

Thank you very much, the code is correct!
I have a lot more columns in my array of arrays (26), but I can't get my for loop to work! Any ideas?
The code below just gives the final column, with blank cells for the first 5 * 25.
for col = 1 : 26
theArray = output_matrix1{col};
xx = cell(5,26);
for row = 1 : size(theArray, 1)
xx{row,col} = theArray(row);
end
end
Hannah, what are you trying to do? Start your own question and attach xx and output_matrix1 and any other needed variables in a .mat file.
Hi,
I am trying to do the same as Carlos, but because I have a 1x26 array, each containing 5x1 vectors, it would be nice to have a for loop.
I needed the data so followed your method above but would like help with a for loop for my write up if you have any ideas.
My question:
My current code is bulky! Eep... But my for loop doesn't work.
% Image Analyst, 2014
% https://uk.mathworks.com/matlabcentral/answers/119476-how-to-export-a-cell-array-into-excel-with-elements-of-different-sizes#comment_728470
theArray = output_matrix1{1}; % Extract first column.
xx = cell(5,26);
for row = 1 : size(theArray, 1)
xx{row,1} = theArray(row);
end
theArray = output_matrix1{2}; % Extract second column.
for row = 1 : size(theArray, 1)
xx{row,2} = theArray(row);
end
theArray = output_matrix1{3}; % Extract third column.
for row = 1 : size(theArray, 1)
xx{row,3} = theArray(row);
end
theArray = output_matrix1{4}; % Extract fourth column.
for row = 1 : size(theArray, 1)
xx{row,4} = theArray(row);
end
theArray = output_matrix1{5}; % Extract fifth column.
for row = 1 : size(theArray, 1)
xx{row,5} = theArray(row);
end
theArray = output_matrix1{6};
for row = 1 : size(theArray, 1)
xx{row,6} = theArray(row);
end
theArray = output_matrix1{7};
for row = 1 : size(theArray, 1)
xx{row,7} = theArray(row);
end
theArray = output_matrix1{8};
for row = 1 : size(theArray, 1)
xx{row,8} = theArray(row);
end
theArray = output_matrix1{9};
for row = 1 : size(theArray, 1)
xx{row,9} = theArray(row);
end
theArray = output_matrix1{10};
for row = 1 : size(theArray, 1)
xx{row,10} = theArray(row);
end
theArray = output_matrix1{11};
for row = 1 : size(theArray, 1)
xx{row,11} = theArray(row);
end
theArray = output_matrix1{12};
for row = 1 : size(theArray, 1)
xx{row,12} = theArray(row);
end
theArray = output_matrix1{13};
for row = 1 : size(theArray, 1)
xx{row,13} = theArray(row);
end
theArray = output_matrix1{14};
for row = 1 : size(theArray, 1)
xx{row,14} = theArray(row);
end
theArray = output_matrix1{15};
for row = 1 : size(theArray, 1)
xx{row,15} = theArray(row);
end
theArray = output_matrix1{16};
for row = 1 : size(theArray, 1)
xx{row,16} = theArray(row);
end
theArray = output_matrix1{17};
for row = 1 : size(theArray, 1)
xx{row,17} = theArray(row);
end
theArray = output_matrix1{18};
for row = 1 : size(theArray, 1)
xx{row,18} = theArray(row);
end
theArray = output_matrix1{19};
for row = 1 : size(theArray, 1)
xx{row,19} = theArray(row);
end
theArray = output_matrix1{20};
for row = 1 : size(theArray, 1)
xx{row,20} = theArray(row);
end
theArray = output_matrix1{21};
for row = 1 : size(theArray, 1)
xx{row,21} = theArray(row);
end
theArray = output_matrix1{22};
for row = 1 : size(theArray, 1)
xx{row,22} = theArray(row);
end
theArray = output_matrix1{23};
for row = 1 : size(theArray, 1)
xx{row,23} = theArray(row);
end
theArray = output_matrix1{24};
for row = 1 : size(theArray, 1)
xx{row,24} = theArray(row);
end
theArray = output_matrix1{25};
for row = 1 : size(theArray, 1)
xx{row,25} = theArray(row);
end
theArray = output_matrix1{26};
for row = 1 : size(theArray, 1)
xx{row,26} = theArray(row);
end
Can you attach your cell array in a .mat file?

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!