我无法在MATLAB中加载文本文件。我正在使用的代码是:
y=load('AllReadings.txt')
哪会产生错误:
文本文件内容是:
Heart Rate (BPM) GSR Respiration Rate Ambient Temperature
inf 495 49.96 3
inf 495 49.96 3
inf 495 23.03 7
inf 496 23.03 7
inf 495 23.03 7
inf 496 23.03 11
7.68 496 23.03 11
7.68 496 23.03 14
7.68 496 23.03 14
7.68 496 23.03 15
7.68 496 23.03 14
(编者注:源数据使用制表符和空格的组合分隔,在渲染输出中不可见,但在编辑问题时可以看到。)
我在R2019a上测试过,可以使用importdata
正确导入这样的文本文件:
>> y = importdata('AllReadings.txt')
y =
struct with fields:
data: [11×4 double]
textdata: {'Heart Rate (BPM) GSR Respiration Rate Ambient Temperature'}
>> y.data
ans =
Inf 495.0000 49.9600 3.0000
Inf 495.0000 49.9600 3.0000
Inf 495.0000 23.0300 7.0000
Inf 496.0000 23.0300 7.0000
Inf 495.0000 23.0300 7.0000
Inf 496.0000 23.0300 11.0000
7.6800 496.0000 23.0300 11.0000
7.6800 496.0000 23.0300 14.0000
7.6800 496.0000 23.0300 14.0000
7.6800 496.0000 23.0300 15.0000
7.6800 496.0000 23.0300 14.0000
这是一个虚拟文件:
header1|header2|header3|header4
adfads|sjk|jkghj|jdauuy2
0987yuh|mnjkhuy6|nmbhgf|0987yuh
098iuhyj|4e5rtyguh|67tyughj|oijk
和导入的代码:
filename = 'dummy.txt';
nCols = 4;
delim = '|';
colFmt = repmat('%s',1,nCols);
fid = fopen(filename,'r');
header = textscan(fid, colFmt, 1, 'delimiter', delim);
dataArray = textscan(fid, colFmt, 'delimiter', delim);
fclose(fid);
dataArray = [dataArray{:}]; % this "unpacks" the cell