有load 和textread两个方法
现有一个data.txt里面的内容是
1 2 3
4 5 6
7 8 9
load
a = load('data.txt')
则
a =
1 2 3
4 5 6
7 8 9
textread
调用方式
[A,B,C,...] = textread('filename','format')
[A,B,C,...] = textread('filename','format',N)
[...] = textread(...,'param','value',...)
输入参数
'filename' 需要读取的数据文件
'format' 每行各个数据格式
N 读取数据时使用N次。
当调用textread函数时会按照指定的格式从'filename'中读取数据,并将数据分别保存在变量A,B,C中,直到文件内容被读完为止。
[a b c] = textread('data.txt', '%d %d %d', 3)
则
a =
1
4
7
b =
2
5
8
c =
3
6
9
对于具有多行文字说明文本数据文件,最好的办法则是用textscan,或者fscanf读取。
调用方式
C = textscan(fid, 'format')
C = textscan(fid, 'format', N)
C = textscan(fid, 'format', param, value, ...)
C= textscan(fid, 'format', N, param, value, ...)