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

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.

2 件のコメント

madhan ravi
madhan ravi 2018 年 10 月 7 日
upload .txt file
ahmad eldeeb
ahmad eldeeb 2018 年 10 月 7 日
Here it is.

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

 採用された回答

Stephen23
Stephen23 2018 年 10 月 7 日
編集済み: Stephen23 2018 年 10 月 7 日

0 投票

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 件)

製品

リリース

R2017b

タグ

質問済み:

2018 年 10 月 7 日

編集済み:

2018 年 10 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by