举例,文本文件data.txt的内容如下:
2.0 3.2 0.1
0.5 0.7 0.9
0.4 0.8 9.8
每行三列,浮点数类型
fileID = fopen('data.txt');
first_line = textscan(fileID, '%f %f %f', 1, 'Delimiter', ' ', 'HeaderLines', 0);
fclose(fileID);
注意1的位置
2021/1/15 更新
对于数值文件(不包含字符串),使用dlmread更方便。因为textscan的返回结果是cell类型,索引相对麻烦。
first_line = dlmread('data.txt', ' ', [0,0,0,2]);