1

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);

2 Answers 2

10

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your help, but, I want to print data as separate columns
@nate I am able to see in proper columns in wordpad but not in notepad...can you suggest why?
notepad unformats anything it handles, wordpad support formatted txt. Try notepad++, or just use in matlab type 'g.txt' etc...
Different text editors can use different tabulation width. If you have values with variable precision (and variable width for representation) than you can have problem with proper column view. dlmwrite support additional parameter 'precision' that can help to correct width of columns.
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.