saving data in a text file

4 ビュー (過去 30 日間)
Nazar Adamchuk
Nazar Adamchuk 2021 年 4 月 28 日
回答済み: Mathieu NOE 2021 年 4 月 28 日
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 件のコメント
Jan
Jan 2021 年 4 月 28 日
Your code looks fine. Which problem do you have with it?

サインインしてコメントする。

回答 (1 件)

Mathieu NOE
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);

カテゴリ

Help Center および File ExchangeLaTeX についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by