登录论坛

查看完整版本 : 如何以给定格式将数据保存到文本文件?


poster
2019-12-10, 16:49
我想将矩阵保存到文本文件,以便可以通过另一个程序读取它。现在,我使用:

save('output.txt', 'A','-ascii'); 但这会将我的文件另存为

6.7206983e+000 2.5896414e-001 6.5710723e+000 4.9800797e-00 6.3466334e+000 6.9721116e-001 5.9975062e+000 1.3346614e+000 6.0224439e+000 1.8127490e+000 6.3466334e+000 2.0517928e+000 6.3965087e+000 1.9721116e+000 但我想保存它们时不要使用“ e-notation”,而不是全部数字。是否有捷径可寻?

编辑:谢谢!那很好。抱歉,但是我认为我使用回滚弄乱了您的编辑。


回答:
我将使用fprintf (https://www.mathworks.com/help/matlab/ref/fprintf.html)函数,该函数将允许您自己定义输出数据所用的格式。例如:

fid = fopen('output.txt', 'wt'); fprintf(fid,'%0.6f %0.6f\n', A.'); fclose(fid); 这将输出精度为小数点后6位的矩阵A。注意,您还必须使用函数fopen (https://www.mathworks.com/help/matlab/ref/fopen.html)和fclose (https://www.mathworks.com/help/matlab/ref/fclose.html) 。



更多&回答... (https://stackoverflow.com/questions/926741)