rcty
rcty

Reputation: 737

How to export data from matlab into a text file

I have multiple arrays of data, out of which only x,y and z arrays are required to be exported as text. I know how to export a single array, but am unable to export the 3 columns of data as text file. please help, I tried with the following..

        fid = fopen('g.txt','w');
        fprintf(fid,'%f \n',x,y,z);
        fclose(fid);

Upvotes: 1

Views: 29676

Answers (2)

Mikhail
Mikhail

Reputation: 8038

You don't want delimiter write, you want csvwrite. It will open nicely in Excel and similar programs.

The following example creates a comma-separated value file from the matrix m.

m = [3 6 9 12 15; 5 10 15 20 25; ...
     7 14 21 28 35; 11 22 33 44 55];

csvwrite('csvlist.csv',m)
type csvlist.csv

3,6,9,12,15
5,10,15,20,25
7,14,21,28,35
11,22,33,44,55

See http://www.mathworks.com/help/matlab/ref/csvwrite.html

Upvotes: 3

bla
bla

Reputation: 26069

try dlmwrite, for example:

x=[1:10]';
y=2*x;
z=3*x;
dlmwrite('g.txt',[x,y,z],'delimiter', '\t');


>type 'g.txt'

1   2   3
2   4   6
3   6   9
4   8   12
5   10  15
6   12  18
7   14  21
8   16  24
9   18  27
10  20  30

Upvotes: 10

Related Questions