如何将数据从matlab导出到文本文件?

时间:2021-08-13 09:46:52

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..

我有多个数据数组,其中只有x、y和z数组作为文本导出。我知道如何导出单个数组,但无法将3列数据导出为文本文件。请帮忙,我试了一下。

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

2 个解决方案

#1


10  

try dlmwrite, for example:

尝试dlmwrite,例如:

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

#2


3  

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

你不想要分隔符写,你想要csvwrite。它将在Excel和类似的程序中很好地打开。

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

下面的例子从矩阵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

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

#1


10  

try dlmwrite, for example:

尝试dlmwrite,例如:

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

#2


3  

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

你不想要分隔符写,你想要csvwrite。它将在Excel和类似的程序中很好地打开。

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

下面的例子从矩阵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

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