Search code examples
matlabexport-to-csv

matlab create a result table to export to csv from data in multiple arrays


I want to export some data so I can visualize it in excel, but I am having trouble right from the header. I would prefer to have numbers in cells (2,11:end), not the whole array.

Num.rand_rot = linspace(pi/36,pi/12,3);
Num.rand_rot = [Num.rand_rot Num.rand_rot Num.rand_rot];
Num.rand_rot = nchoosek(Num.rand_rot,3);
Num.rand_rot = unique(Num.rand_rot, 'rows', 'stable');
[Num.rand_rot2,~] = size(Num.rand_rot);
Num.objects = 25;
results=cell(Num.objects+2,Num.rand_rot2*3+10);
results(1,1:11)={'Object','Initial','','','Measured angles','','','E for measured angles','','','E for new little rotation angles';};
results(2,11:end)={reshape(Num.rand_rot.',1,[]);};

Solution

  • Found the problems:

    -one ";" was not needed

    -but cell conversion was required

    results(2,11:end)=num2cell(reshape(Num.rand_rot.',1,[]));

    Also this could be written to a csv if each column had an unique variable name:

    T = cell2table(results(2:end,:),'VariableNames',results(1,:));
    writetable(T,'myDataFile.csv');
    

    Or what I did was just keep the table as a matrix not a cell array:

    results           = zeros(Num.Mols+1, Num.rand_rot2*3+10);
    results(1,11:end) = reshape(Num.rand_rot.', 1, []);
    csvwrite(['results/results.csv'], results);