matlab中将数据(多数组)输出保存为txt格式文件的方法(fopen的方法)

z.B

两个序列

n=0:15;

x1=3*cos(0.125*pi*n+0.2*pi)+2*sin(0.25*pi*n+0.1*pi);

希望输出的时候可以一一对应

所以采用一下方法,同时输出两个序列,并且能一一对照

>>data=[n;x1]                             %用data将n和x1集合成一个2*16的数组

>>fileID=fopen('sn1.txt','w');                 %打开文件 返回文件id号

 

>>fprintf(fileID,'n x1\n');                    %输出字符

>>fprintf(fileID,'%d %.5f\n',data);             %输出集合数组data

>>fclose(fileID);                           %关闭文件

%注意,txt文件的写入是先写所有行的第一列,再是所有行的第二列等等

 

其中data数组是一个2行*16列的数组

data =

  Columns 1 through 4

         0    1.0000    2.0000    3.0000
    3.0451    3.3495    2.3714    0.2076

  Columns 5 through 8

    4.0000    5.0000    6.0000    7.0000
   -2.3814   -4.3399   -4.8652   -3.8251

  Columns 9 through 12

    8.0000    9.0000   10.0000   11.0000
   -1.8090    0.2145    1.4328    1.6083

  Columns 13 through 16

   12.0000   13.0000   14.0000   15.0000
    1.1453    0.7759    1.0610    2.0091

 

得到结果

matlab中将数据(多数组)输出保存为txt格式文件的方法(fopen的方法)

 

matlab中将数据(多数组)输出保存为txt格式文件的方法(fopen的方法)