saving data in a text file
조회 수: 1 (최근 30일)
이전 댓글 표시
I have this from the matlab reference manual
fprintf(fileID, formatSpec, A1,...,An)
where formatSpec defines how I write data to a text file.
I have struct with two fields (the .m-file attached). I want to write the data into a text file in this way:
Temparature Curves:
Thermal_Conductivity 3
1500 10
1400 9.5
1300 9
Density 4
1500 1000
1450 975
1400 965
1350 960
Firstly, the field name, then the number of data pairs. Then goes the first themperature curve. After the first curve finishes, the same happens with the second one.
fileID = fopen('output.txt','w');
names = fieldnames(data);
fprintf(fileID, 'Temparature Curves:');
for i = 1:length(names)
fprintf(fileID, '\n%s %g ', names{i}, size(data.(names{i}),1));
fprintf(fileID, '\n%g %g', data.(names{i}));
end
fclose(fileID);
I am stuck with the way to do it. My "home made solution" is a for loop. Maybe you know the way how to do it simpler and better understandable?
답변 (1개)
Mathieu NOE
2021년 4월 28일
hi again
you were one micro inch from the solution : simply add the transpose operation on the data matrix to get it oriented the right way :
fileID = fopen('output.txt','w');
names = fieldnames(data);
fprintf(fileID, 'Temparature Curves:');
for i = 1:length(names)
fprintf(fileID, '\n%s %g ', names{i}, size(data.(names{i}),1));
fprintf(fileID, '\n%g %g', (data.(names{i}))'); % look here !! transpose data
end
fclose(fileID);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!