How to export a Matlab cell array to an Excel spreadsheet?

144 次查看(过去 30 天)
I have a cell array X={'A','B','C','D',A,B,C,D} where A,B,C and D are column vectors of equal size. I intended to create a spreadsheet by: >>xlswrite('X.xls',X) but I didnt get the values of the vectors in my spreadsheet. Maybe I didn't create the cell array properly (when I display the cell array, I get: >> X X =
'A' 'B' 'C' 'D'
{241x1 cell} {241x1 cell} {241x1 cell} {241x1 cell}
Can anyone provide any suggestions? Thanks.

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2013-11-24
编辑:Azzi Abdelmalek 2013-11-24
X=[{'A','B','C','D'};A,B,C,D]
xlswrite('X.xls',X)
  4 个评论
Rinu
Rinu 2013-11-24
It worked! Thanks! The problem was I defined A,B,C and D as numerical arrays instead of as cell arrays. I now converted them to cell arrays using num2cell.
Rini
Rini 2014-10-16
Is there a way to iteratively write a large cell array to excel file? I have an cell array of 20000 by 1 and in each cell there are varied number of strings. I tried xlswrite (filename,A) but it did nothing. Thanks!

请先登录,再进行评论。

更多回答(2 个)

Image Analyst
Image Analyst 2013-11-24
It looks like A, B, C, and D are actually cell arrays, not regular numerical arrays. How did you create A, B, C, and D? Did you put braces around them when you created them? If so, you don't want to do that.
A = rand(5); % OK. A is a double array.
A = {rand(5)}; % Not okay - A is now a cell.
  3 个评论
Image Analyst
Image Analyst 2013-11-24
Yeah you're right. Now that I think about it, it can write out a numerical array, but if you're going to combine them (strings plus numbers) then you need to have one big cell array where each cell is just one number (element) from the numerical array, not the whole array. So you'd need to do
ca = cell(25, 4);
ca{1,1} = 'A';
ca{1,2} = 'B';
ca{1,3} = 'C';
ca{,14} = 'D';
for k = 2:25
ca{1, k} = A(k-1);
ca{2, k} = B(k-1);
ca{3, k} = C(k-1);
ca{4, k} = D(k-1);
end

请先登录,再进行评论。


João Araújo
João Araújo 2017-10-18
I have a follow up question. I'm looking to organize some data, and I have a cell array with some names. The cells aren't necessarily the same size, since I can have 5 names in one cell and 12 in the next. I have a total of 1783 cells, so I'm looking for an automated way to solve this issue. How can I export this cell array to an excel spreadsheet?

标签

Community Treasure Hunt

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

Start Hunting!

Translated by