How to save this matrix?

1 回表示 (過去 30 日間)
Amanda Queiroz
Amanda Queiroz 2021 年 12 月 24 日
編集済み: Amanda Queiroz 2021 年 12 月 28 日
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];

採用された回答

dpb
dpb 2021 年 12 月 24 日
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 件のコメント
Amanda Queiroz
Amanda Queiroz 2021 年 12 月 27 日
編集済み: Amanda Queiroz 2021 年 12 月 28 日
Thank you, these commands got it right!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by