Main Content

将数据写入文本文件

将表、元胞数组或数值数组中包含的表格数据从 MATLAB® 工作区导出到文本文件。

将表导出到文本文件

可使用 writetable 函数将表格数据从 MATLAB® 工作区导出到文本文件。创建样本表,将表写入文本文件,然后指定更多选项并再次将表写入文本文件。

创建包含变量 PitchShapePriceStock 的样本表 T

Pitch = [0.7;0.8;1;1.25;1.5];
Shape = {'Pan';'Round';'Button';'Pan';'Round'};
Price = [10.0;13.59;10.50;12.00;16.69];
Stock = [376;502;465;1091;562];
T = table(Pitch,Shape,Price,Stock)
T=5×4 table
    Pitch      Shape       Price    Stock
    _____    __________    _____    _____

     0.7     {'Pan'   }       10     376 
     0.8     {'Round' }    13.59     502 
       1     {'Button'}     10.5     465 
    1.25     {'Pan'   }       12    1091 
     1.5     {'Round' }    16.69     562 

将表 T 导出到一个名为 tabledata.txt 的文本文件。查看文件的内容。默认情况下,writetable 会写入逗号分隔的数据,将表变量名称作为列标题。

writetable(T,'tabledata.txt');
type tabledata.txt
Pitch,Shape,Price,Stock
0.7,Pan,10,376
0.8,Round,13.59,502
1,Button,10.5,465
1.25,Pan,12,1091
1.5,Round,16.69,562

创建表 T2,使用 RowNames 名称-值对组参量指定行名称。

rowNames = {'M4';'M5';'M6';'M8';'M10'};
T2 = table(Pitch,Shape,Price,Stock,'RowNames',rowNames)
T2=5×4 table
           Pitch      Shape       Price    Stock
           _____    __________    _____    _____

    M4      0.7     {'Pan'   }       10     376 
    M5      0.8     {'Round' }    13.59     502 
    M6        1     {'Button'}     10.5     465 
    M8     1.25     {'Pan'   }       12    1091 
    M10     1.5     {'Round' }    16.69     562 

T2 导出到名为 tabledata2.txt 并以制表符分隔的文本文件。使用 Delimiter 名称-值对组参量指定制表符分隔符,并使用 WriteRowNames 名称-值对组参量以包括行名称。查看文件的内容。

writetable(T2,'tabledata2.txt','Delimiter','\t','WriteRowNames',true);
type tabledata2.txt
Row	Pitch	Shape	Price	Stock
M4	0.7	Pan	10	376
M5	0.8	Round	13.59	502
M6	1	Button	10.5	465
M8	1.25	Pan	12	1091
M10	1.5	Round	16.69	562

将元胞数组导出到文本文件

可采用以下方法之一,将元胞数组从 MATLAB® 工作区导出到文本文件:

  • 使用 writecell 函数将元胞数组导出到文本文件。

  • 通过指定输出数据的格式,使用 fprintf 导出元胞数组。

创建样本元胞数组 C

C = {'Atkins',32,77.3,'M';'Cheng',30,99.8,'F';'Lam',31,80.2,'M'}
C = 3×4 cell array
    {'Atkins'}    {[32]}    {[77.3000]}    {'M'}
    {'Cheng' }    {[30]}    {[99.8000]}    {'F'}
    {'Lam'   }    {[31]}    {[80.2000]}    {'M'}

使用 writecell 导出元胞数组。

writecell(C,'data.dat')

查看文件的内容。

type data.dat
Atkins,32,77.3,M
Cheng,30,99.8,F
Lam,31,80.2,M

或者,使用 fprintf 导入元胞数组。打开一个可供写入的名为 celldata.dat 的文件。使用格式设定符定义 formatSpec,以描述文件中的数据模式。典型的格式设定符包括:表示字符向量的 '%s',表示整数的 '%d' 或者表示浮点数的 '%f'。使用空格分隔每个格式设定符,以指示对输出文件使用空格分隔符。在每行数据的末尾包括换行符 ('\n')。

fileID = fopen('celldata.dat','w');
formatSpec = '%s %d %2.1f %s\n';

确定 C 的大小,并使用 fprintf 函数一次导出一行数据。然后关闭文件。fprintf 将写入一个空格分隔的文件。

[nrows,ncols] = size(C);
for row = 1:nrows
    fprintf(fileID,formatSpec,C{row,:});
end
fclose(fileID);

查看文件的内容。

type celldata.dat
Atkins 32 77.3 M
Cheng 30 99.8 F
Lam 31 80.2 M

将数值数组导出到文本文件

可使用 writematrix 将数值数组导出到文本文件。

创建数值数组 A。

A = magic(5)/10 
A = 5×5

    1.7000    2.4000    0.1000    0.8000    1.5000
    2.3000    0.5000    0.7000    1.4000    1.6000
    0.4000    0.6000    1.3000    2.0000    2.2000
    1.0000    1.2000    1.9000    2.1000    0.3000
    1.1000    1.8000    2.5000    0.2000    0.9000

将该数值数组写入到 myData.dat,并将分隔符指定为 ';'。然后,查看文件的内容。

writematrix(A,'myData.dat','Delimiter',';')  
type myData.dat
1.7;2.4;0.1;0.8;1.5
2.3;0.5;0.7;1.4;1.6
0.4;0.6;1.3;2;2.2
1;1.2;1.9;2.1;0.3
1.1;1.8;2.5;0.2;0.9

另请参阅

| | | | |