Save different size cells to text file
2 views (last 30 days)
Show older comments
This post is building from previous posts and I have been modifying the code as I go along. I am still having a problem saving the different size cells to a text file. I have created the cell, called "CELL" with the different arrays. How now do I save that cell to a CSV dat file?
Here is my modified 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;
T = cell2table(CELL,'VariableNames',{'Iter','B','Iter2','D','E'});
writetable(T,'tabledata.dat')
type tabledata.dat
% fileID = fopen('check.txt','w');
% fprintf(fileID,'%6s %6s %6s %6s %6s\r\n','a','b', 'c', 'd', 'e');
% fprintf(fileID,'%6.5f %6.5f %6.5f %6.5f %6.5f\r\n',CELL);
% fclose(fileID);
0 Comments
Answers (1)
dpb
on 19 Nov 2015
>> for i=1:length(CELL)
l=length(CELL{i});
fmt=[repmat('%8.3f',1,l) '\n'];
fprintf(fmt,cell2mat(CELL{i}))
end
1.000 2.000 3.000 4.000 5.000 6.000 7.000 8.000 9.000 10.000 11.000 12.000 13.000 14.000 15.000
0.375 0.418 0.161 0.276 0.490 0.275 0.165 0.310 0.180 0.378 0.207 0.246 0.347 0.486 0.164
1.000 2.000 3.000 4.000 5.000 6.000 7.000 8.000 9.000 10.000 11.000 12.000 13.000 14.000 15.000 16.000 17.000 18.000 19.000 20.000
0.419 0.370 0.477 0.016 0.178 0.331 0.141 0.115 0.356 0.312 0.295 0.330 0.024 0.174 0.226 0.120 0.358 0.428 0.141 0.366
0.069 0.418 0.069 0.294 0.183 0.403 0.252 0.245 0.439 0.177 0.225 0.482 0.021 0.486 0.095 0.334 0.293 0.338 0.181 0.310
>>
Cell arrays and i/o are a bear, granted since fprintf and friends haven't been enhanced to handle them transparently. The reason being, "it's tough" because they can have anything in 'em in any size.
You make it even more tough than it need be here by the use of num2cell that makes each an array of arrays of one element each so that have to use cell2mat to get back the vectors in the loop to pass them to fprintf.
If you instead made CELL by
CELL{1}=A;
CELL{2}=B;
...
etc., then each cell would be a double array and you'd retrieve it by simply the curly braces around the subscript. That removes one level of indirection.
The difference is
>> Cell{1}=A; Cell{2}=B; % make a sample other cell array from components...
>> Cell
Cell =
[1x15 double] [15x1 double]
>> CELL
CELL =
{15x1 cell} {15x1 cell} {20x1 cell} {20x1 cell} {20x1 cell}
>>
2 Comments
dpb
on 20 Nov 2015
Edited: dpb
on 20 Nov 2015
Well, not really...you seem to have used writetable instead of low-level output as I illustrated.
I don't have a release that includes the table data type so I've no input on it, sorry. As far as other suggestions, I can only refer to the doc on outputting cell arrays
write-to-delimited-data-files for TMW guidance. It wouldn't surprise me as it's still pretty new that if the table structure is complicated there are still "issues" with writetable for it; I can only suggest using the example as a practice run to get the basics down and then try to expand it for your case. As noted above, the double-nested cell structure is pretty hard to get to even w/ low-level functions so working to simplify the storage would likely help all around.
See Also
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!