从ASCII文件导入表

时间:2019-04-17 07:58:12

标签: matlab import ascii tabular file-read

我无法在MATLAB中加载文本文件。我正在使用的代码是:

y=load('AllReadings.txt')

哪个会产生错误: enter image description here

文本文件的内容为:

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
  

(编者注:使用制表符和空格的组合来分隔源数据,这在呈现的输出中不可见,但在编辑问题时可以看到。)

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

响应linked question

这是一个虚拟文件:

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

在工作区中看起来像这样: enter image description here