How to print an array to a .txt file in Matlab?

我是研究僧i 提交于 2019-12-04 18:25:47

问题


I am just beginning to learn Matlab, so this question might be very basic:

I have a variable

a=[2.3 3.422 -6.121 9 4.55]

I want the values to be output to a .txt file like this:

2.3
3.422
-6.121
9
4.55

How can I do this?

fid = fopen('c:\\coeffs.txt','w'); //this opens the file
//now how to print 'a' to the file??

回答1:


The following should do the trick:

fid = fopen('c:\\coeffs.txt','wt');  % Note the 'wt' for writing in text mode
fprintf(fid,'%f\n',a);  % The format string is applied to each element of a
fclose(fid);

For more info, check out the documentation for FOPEN and FPRINTF.



来源:https://stackoverflow.com/questions/1456882/how-to-print-an-array-to-a-txt-file-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!