Save Cell data to file
Show older comments
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);
3 Comments
Walter Roberson
on 20 Nov 2015
What results are you expecting? Your CELL{1} and CELL{2} will be 15 x 1, but your other CELL entries are 20 x 1 . Do you want empty entries for the first two columns for the last 5 rows?
Walter Roberson
on 20 Nov 2015
This seems to be continuing http://uk.mathworks.com/matlabcentral/answers/256535-save-different-size-cells-to-text-file
monkey_matlab
on 20 Nov 2015
Edited: monkey_matlab
on 20 Nov 2015
Accepted Answer
More Answers (1)
Image Analyst
on 20 Nov 2015
Edited: Image Analyst
on 20 Nov 2015
Try this:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
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','wt');
fprintf(fileID,'%6s, %6s, %6s, %6s, %6s\n','Iter1','b', 'Iter2', 'd', 'e');
fprintf(fileID,'%6.5f, %6.5f, %6.5f, %6.5f, %6.5f\n',CELL{1});
fprintf(fileID,'%6.5f, %6.5f, %6.5f, %6.5f, %6.5f\n',CELL{2});
fprintf(fileID,'%6.5f, %6.5f, %6.5f, %6.5f, %6.5f\n',CELL{3});
fprintf(fileID,'%6.5f, %6.5f, %6.5f, %6.5f, %6.5f\n',CELL{4});
fprintf(fileID,'%6.5f, %6.5f, %6.5f, %6.5f, %6.5f\n',CELL{5});
fclose(fileID);
4 Comments
monkey_matlab
on 20 Nov 2015
Edited: monkey_matlab
on 20 Nov 2015
Image Analyst
on 20 Nov 2015
Either see if Walter's solution works, or give an example of what you want to the output, because I don't know what it means to have the last 5 entries empty in the first two columns.
monkey_matlab
on 20 Nov 2015
Walter Roberson
on 20 Nov 2015
Is your goal to have column-oriented output, or is your goal to have a csv file? Because a csv file would look like
"Iter1","b","Iter2","d","e","f"
1,.05120,1,.8512,.5656,5632
2,.56355,2,.5635,.1259,.6321
...
15,.1256,15,.8632,.4425,.0856
,,16,.2265,.4863,.9652
...
,,20,.1125,.9965,.1256
Categories
Find more on Database Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!