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!