필터 지우기
필터 지우기

How write a struct into a text file?

조회 수: 7 (최근 30일)
Donghui  Sun
Donghui Sun 2013년 12월 12일
댓글: Donghui Sun 2013년 12월 12일
I have a 100-by-1 struct array, named fInfo. fInfo have four fields:a1,a2,a3,a4. For example, the fInfo(1,1) is
fInfo(1,1).a1 = [89 78 90 56];
fInfo(1,1).a2 = 100
fInfo(1,1).a3 = 20
fInfo(1,1).a4 = 'small'
What should I do to write the struct into a text file? I have tried this:
fid = fopen('result.txt', 'wt+');
for i = 1:size(fInfo, 1)
fprintf(fid, '%s\t %d\t %d\t %s\n',fInfo(i,1).a1,fInfo(i,1).a2,fInfo(i,1).a3,fInfo(i,1).a4,);
end
fclose(fid);
But,it not works.
Any suggestions?

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 12월 12일
fid = fopen('result.txt', 'wt+');
for i = 1:size(fInfo, 1)
fprintf(fid, '%d\t',fInfo(i,1).a1),
fprintf(fid, '%d\t',fInfo(i,1).a2)
fprintf(fid, '%d\t',fInfo(i,1).a3)
fprintf(fid, '%s\n',fInfo(i,1).a4)
end
fclose(fid);
  댓글 수: 1
Donghui  Sun
Donghui Sun 2013년 12월 12일
Thanks, It works. I find another way. Firstly, transform the a1 to string,and then use the fprintf. :-)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by