为处理文本文件,MATLAB提供了多种处理函数,常见的函数包括csvread,dlmread和testread等。
1. csvwrite函数
csvwrite函数写入数据时每一行以换行符结束,函数不反悔任何值。csvwrite函数的调用格式如下:
- csvwrite(‘filename’,M):将数组M 中的数据保存为文件filename,数据间以逗号分隔
- csvwrite(‘filename’,M,row,col):将数组 M 中的指定数据保存在文件中,数据由参数
row 和 col 指定,保存 row 和 col 右下角的数据
clear all
clc
m=[1,2,3;4,5,6;,7,8,9];
csvwrite('test.dat',m);
type test.dat;
命令行窗口的输出结果为:
1,2,3
4,5,6
7,8,9
将数组 m 写入 csvlist.dat 文件中,并在数据链前添加两个数据列
clear all
clc
m=[1,2,3;4,5,6;,7,8,9];
csvwrite('test.dat',m,0,2);
type test.dat;
命令行窗口的输出结果为:
,,1,2,3
,,4,5,6
,,7,8,9
将数组 m 写入 csvlist.dat 文件中,并在数据链前添加 2个数据列和在数据列上方添加两个数据行
clear all
clc
m=[1,2,3;4,5,6;,7,8,9];
csvwrite('test.dat',m,2,2);
type test.dat;
命令行窗口的输出结果为:
,,,,
,,,,
,,1,2,3
,,4,5,6
,,7,8,9
2. csvread函数
csvread函数的调用格式如下:
- M = csvread(‘filename’):将文件 filename 中的数据读入,并且保存为 M。filename中只能包含数字,并且数字之间以逗号分隔。M 是一个数组,行数与 filename 的行数相同,列数为 filename 列的最大值;对于元素不足的行,以 0 补充。
- M = csvread(‘filename’, row, col):读取文件 filename 中的数据,起始行为 row,起始列为 col。需要注意的是,此时的行、列从 0 开始。
- M = csvread(‘filename’, row, col, range):读取文件 filename 中的数据,起始行为 row,起始列为col,读取的数据由数组 range 指定,range 的格式为[R1 C1 R2 C2],其中 R1、C1 为读取区域左上角的行和列,R2、C2 为读取区域右下角的行和列
A=csvread('test.dat')
B=csvread('test.dat',1,1)
C=csvread('test.dat',2,2,[2,2,3,3])
命令行窗口的输出结果为:
A =
0 0 0 0 0
0 0 0 0 0
0 0 1 2 3
0 0 4 5 6
0 0 7 8 9
B =
0 0 0 0
0 1 2 3
0 4 5 6
0 7 8 9
C =
1 2
4 5
3. dlmwrite函数
dlmwrite函数用于向文档中写入数据,dlmwrite函数的调用格式如下:
- dlmwrite(‘filename’, M):将矩阵 M 的数据写入文件 filename 中,以逗号分隔
- dlmwrite(filename, M, ‘D’):将矩阵 M 的数据写入文件 filename 中,采用指定的分隔符分隔数据,如果需要 Tab 键,可以用“\t”指定
- dlmwrite(‘filename’, M, ‘D’, R, C):指定写入数据的起始位置
- dlmwrite(filename, M, ‘attrib1’, value1, ‘attrib2’, value2, …):指定任意数目的参数,可以指定的参数如下表
- dlmwrite(‘filename’, M,’-append’):如果 filename 指定的文件存在,则在文件后面写入数据,不指定时则覆盖源文件
- dlmwrite(‘filename’, M,’-append’, attribute-value list):续写文件,并指定参数
| 参数名 | 功能 |
|---|---|
| delimiter | 用于指定分隔符 |
| newline | 用于指定换行符,可以选择”PC”或者”UNIX“ |
| roffset | 行偏差,指定文件第一行的文字,roffset的基数为0 |
| coffser | 列偏差,指定第一列的位置,coffset的基数为0 |
| precision | 指定精确度,可以指定精确维数 |
应用示例:
m=rand(4)
dlmwrite('test1.txt',m,'delimiter','\t','precision',5)
type test1.txt
dlmwrite('test2.txt',m,'delimiter','\t','precision',2)
type test2.txt
结果为:
0.81472 0.63236 0.95751 0.95717
0.90579 0.09754 0.96489 0.48538
0.12699 0.2785 0.15761 0.80028
0.91338 0.54688 0.97059 0.14189
0.81 0.63 0.96 0.96
0.91 0.098 0.96 0.49
0.13 0.28 0.16 0.8
0.91 0.55 0.97 0.14
向文件中写入多行数据
M=magic(3)
dlmwrite('test3.txt',[M*4 M/4],' ')
type test3.txt
dlmwrite('test3.txt',rand(3),'-append','roffset',1,'delimiter',' ')
结果:
%第一次输出结果
32 4 24 2 0.25 1.5
12 20 28 0.75 1.25 1.75
16 36 8 1 2.25 0.5
%第二次输出结果
32 4 24 2 0.25 1.5
12 20 28 0.75 1.25 1.75
16 36 8 1 2.25 0.5
0.42176 0.95949 0.84913
0.91574 0.65574 0.93399
0.79221 0.035712 0.67874
4. dlmread函数
dlmread函数用于从文档中读入数据,调用格式为:
- M = dlmread(filename)
- M = dlmread(filename, delimiter)
- M = dlmread(filename, delimiter, R, C)
- M = dlmread(filename, delimiter, range)
其中,参数 delimiter 用于指定文件中的分隔符;其他参数的意义与 csvread 函数中参数的意义相同,这里不再赘述。dlmread 函数与 csvread 函数的差别在于,dlmread 函数在读入数据时可以指定分隔符,不指定时默认分隔符为逗号。
5. testread函数
当文件的格式已知时,可以利用textread和textscan函数读入。
- [A,B,C,…] = textread(filename,format)
- [A,B,C,…] = textread(filename,format,N)
其中,format 可以是%d、%f、%s 等。
读取test4.txt的内容,其内容为:test no4 12.12 34 true
[string,no,x,y,flag]=textread('test4.txt',...
'%s %s %f %d %s',1)
结果为:
string =
'test'
no =
'no4'
x =
12.1200
y =
34
flag =
'true'
读取test4.txt的内容,但no仅显示数字:
[string,no,x,y,flag]=textread('test4.txt',...
'%s no%s %f %2d %s',1)
结果为:
string =
'test'
no =
'4'
x =
12.1200
y =
34
flag =
'true'
本文详细介绍了MATLAB中用于读写文本文件的几个函数,包括csvwrite用于写入CSV格式数据,csvread用于读取CSV格式数据,dlmwrite和dlmread分别用于以特定分隔符写入和读取数据,以及textread函数在已知文件格式时的使用。示例展示了如何使用这些函数进行数据的存取操作。
6268

被折叠的 条评论
为什么被折叠?



