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

2020-06-14 07:06发布

问题:

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.