How to save this matrix?

2 views (last 30 days)
Amanda Queiroz
Amanda Queiroz on 24 Dec 2021
Edited: Amanda Queiroz on 28 Dec 2021
I have 12 columns in matrix:
For example:
6,2713 1,0876 23,3585 31,5388 51,1182 27,1688 9,6255 0,7503 0 0 0 0,3299 -5,8128 -37,5098
6,0273 1,0467 22,9448 31,1567 48,5899 26,2585 9,6405 0,7245 0 0 0 0,3299 -5,8173 -37,5098
5,6551 0,9194 22,7827 29,8986 47,5964 24,7148 9,7817 0,6674 0 0 0 0,3299 -5,8217 -37,5098
How to save this matrix with fprintf for txt file?
I tried
fileID = fopen('test.txt','w');
fprintf(fileID,'%.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f\n',[a,b,c,d,e,f,g,h,i,j,k,l,m,n];

Accepted Answer

dpb
dpb on 24 Dec 2021
If there's just one array/matrix M, then
fid=fopen('test.txt','w');
fmt=[repmat('%.4f',1,size(M,2)) '\n']; % build format string
fprintf(fid,fmt,M.') % write -- NB: to transpose
fid=fclose(fid);
But, I'd recommend using one of the higher-level routines instead of the low-level i/o unless the desired file format is one that can't be written otherwise. See
doc writematrix
and friends...
  1 Comment
Amanda Queiroz
Amanda Queiroz on 27 Dec 2021
Edited: Amanda Queiroz on 28 Dec 2021
Thank you, these commands got it right!

Sign in to comment.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices 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!