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 CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by