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 件のコメント
回答 (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!