How write a struct into a text file?

10 Ansichten (letzte 30 Tage)
Donghui  Sun
Donghui Sun am 12 Dez. 2013
Kommentiert: Donghui Sun am 12 Dez. 2013
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?

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 12 Dez. 2013
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 Kommentar
Donghui  Sun
Donghui Sun am 12 Dez. 2013
Thanks, It works. I find another way. Firstly, transform the a1 to string,and then use the fprintf. :-)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by