필터 지우기
필터 지우기

How to write matrix to txt or excel file with specific precision

조회 수: 23 (최근 30일)
ahmad eldeeb
ahmad eldeeb 2018년 10월 7일
편집: Stephen23 2018년 10월 7일
I have a 100*100 matrix that I need to save it in a txt or excel file with high precision. This the code I use:
fid = fopen('data.txt', 'w+');
for j = 1:size(s, 2)
for i = 1:size(s, 1)
fprintf(fid, '%2.20f \n', s(i,j));
end
end
The problem in the output, it saves all the values in one line. Any ideas.

채택된 답변

Stephen23
Stephen23 2018년 10월 7일
편집: Stephen23 2018년 10월 7일
You don't need a loop. Try this:
M = your matrix
fmt = repmat(',%2.20f',1,size(M,2));
fmt = [fmt(2:end),'\n'];
[fid,msg] = fopen('data.csv', 'wt');
assert(fid>=3,msg)
fprintf(fid,fmt,M.')
fclose(fid);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by