How to save this matrix?

5 vues (au cours des 30 derniers jours)
Amanda Queiroz
Amanda Queiroz le 24 Déc 2021
Modifié(e) : Amanda Queiroz le 28 Déc 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];

Réponse acceptée

dpb
dpb le 24 Déc 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 commentaire
Amanda Queiroz
Amanda Queiroz le 27 Déc 2021
Modifié(e) : Amanda Queiroz le 28 Déc 2021
Thank you, these commands got it right!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by