Search code examples
matlabimportasciitabularfile-read

Importing a table from an ASCII file


I am unable to load a text file in MATLAB. The code I'm using is:

y=load('AllReadings.txt')

Which produces the error: enter image description here

The text file contents are:

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.)


Solution

  • I tested it on R2019a, and such a text file can be imported correctly using 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