Search code examples
matlabexport-to-excel

Export nested cell array from matlab to excel


I have a nested cell array where each cell has three columns but different number of rows. I need to export the values to excel keeping the columns intact but using writecell gives me an error 'Nested cell arrays are not supported'. How can I export the values to excel?

Here's a quick example:

A1 = {1,1,1};
A2 = {2,2,2;2,2,2};
A3 = {3,3,3;3,3,3;3,3,3};
A = {A1, A2, A3}; 
writecell(A, 'mydata.xlsx');

Solution

  • A1 = {1,1,1};
    A2 = {2,2,2;2,2,2};
    A3 = {3,3,3;3,3,3;3,3,3};
    A = {A1, A2, A3};
    [~,NoCells]=size(A);
    for aa=1:NoCells
    writecell(A{aa}, 'mydata.xlsx','WriteMode','append');
    end
    

    edit: as suggested by wolfie in the comments the code can be shortened to:

    A1 = {1,1,1};
    A2 = {2,2,2;2,2,2};
    A3 = {3,3,3;3,3,3;3,3,3};
    A = {A1, A2, A3};
    for aa=1:size(A,2)
    writecell(A{aa}, 'mydata.xlsx','WriteMode','append');