MATLAB: Save Cell data to file

cell arraysdata

Hello,
In the code below, how do I go about saving the cell data to a csv file with the specified headers?
Here is my code:
clc;clear;
A = 1:15;
B = (0.5)*rand(15,1);
C = 1:20;;
D = (0.5)*rand(20,1);
E = (0.5)*rand(20,1);
CELL{1} = A';
CELL{2} = B;
CELL{3} = C';
CELL{4} = D;
CELL{5} = E;
% fileID = fopen('check.dat','w');
% fprintf(fileID,'%6s %6s %6s %6s %6s\r\n','Iter1','b', 'Iter2', 'd', 'e');
% fprintf(fileID,'%6.5f %6.5f %6.5f %6.5f %6.5f\r\n',CELL);
% fclose(fileID);

Best Answer

Create a cell array large enough to hold everything if everything was the same length -- so in this case, 20 by 5. Set all the entries to '' the empty string. Now in the entries that should have a defined value, write in the string corresponding to the number that goes in that position. For example,
Astr = cellstr(num2str(A.', '%6.5f'));
Output(1:length(Astr), 1) = Astr;
Once the entire Output cell array has been populated,
fmt = '%s,%s,%s,%s,%s\n';
OutTranspose = Output .'; %need to transpose it
fprintf(fileID, fmt, OutTranspose{:});
The transpose has to do with row versus column concerns.