views:

1236

answers:

1

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??
+2  A: 

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.

gnovice
@gnovice yes, it does. Thanks. Do you know some place where I can lookup for some basic stuff like this. Its because I just started with Matlab and I often find myself stuck at such trivial stuff.
Lazer
I thought I will need a loop for that. somehow all the values are printed by itself!!
Lazer
@eSKay: I added a couple of links for the pertinent functions. In general, MATLAB has very good documentation and examples/tutorials, both with the version and online at The MathWorks website (http://www.mathworks.com). In the command window, the HELP command (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/help.html) is usually very, well, helpful. ;)
gnovice
@gnovice getting incorrect results in input, posted as a separate ques because it required formatting http://stackoverflow.com/questions/1457177/how-to-read-input-from-a-text-file-in-matlab
Lazer
Would someone like to explain why they downvoted this?
gnovice