从 ASCII 文件导入 table

Importing a table from an ASCII file

我无法在 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

(Editor's note: the source data is delimited using a combination of tabs and spaces, which is not visible in the rendered output, but can be seen when editing the question.)

我在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

回应linked question

这是一个虚拟文件,因为 OP 没有任何数据:

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

在工作区中看起来像这样: