필터 지우기
필터 지우기

Save different size cells to text file

조회 수: 3 (최근 30일)
monkey_matlab
monkey_matlab 2015년 11월 19일
편집: dpb 2015년 11월 20일
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);

답변 (1개)

dpb
dpb 2015년 11월 19일
>> 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
monkey_matlab
monkey_matlab 2015년 11월 19일
Modified my code per your recommendation. when I try to save the data file, it is totally mixed up. This is what I should get:
Iter1, B, Iter2, D, E\r\n 15x1, 15x1, 20x1, 20x1, 20x1
I am still not getting the correct output. Can you offer some more suggestions? Thank you!
dpb
dpb 2015년 11월 20일
편집: dpb 2015년 11월 20일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by