saving data in a text file

조회 수: 1 (최근 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 CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by