How write a struct into a text file?
5 views (last 30 days)
Show older comments
Donghui Sun
on 12 Dec 2013
Commented: Donghui Sun
on 12 Dec 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?
0 Comments
Accepted Answer
Azzi Abdelmalek
on 12 Dec 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);
More Answers (0)
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!