matlabtimetable

Error when writing and then reading a timetable


I have a Matlab timetable that I would like to save so I can read it at a later time in a different script.

I would expect the following minimal example to work but it doesn't:

% Dummy table
RowTimes = seconds(1:5)';
TT = timetable(RowTimes,[98;97.5;97.9;98.1;97.9],[120;111;119;117;116],...
               'VariableNames',{'Reading1','Reading2'})

% Save
writetimetable(TT)

% Load
TT2 = readtimetable('TT.txt')

Throws: Expected a datetime or duration variable to use as RowTimes. Check that the data can be converted to datetime or duration, or supply a "TimeStep" or "SampleRate".

When including a "TimeStep":

TT2 = readtimetable('TT.txt', "TimeStep", seconds(1))

returns

TT2 =

  5×3 timetable

      Time      RowTimes     Reading1    Reading2
    ________    _________    ________    ________

    00:00:00    {'1 sec'}        98        120   
    00:00:01    {'2 sec'}      97.5        111   
    00:00:02    {'3 sec'}      97.9        119   
    00:00:03    {'4 sec'}      98.1        117   
    00:00:04    {'5 sec'}      97.9        116 

So without an error but with an additional column 'RowTimes' which one would think satisfies the demand of the previously thrown error.

I could just delete 'RowTimes' and call it a day but this makes me think that there must be a better way.


Solution

  • Your RowTimes are being written to TT.txt in the format 1 sec, 2 sec etc, which is not a valid format for readtimetable (tested in R2022b) to understand what the time is on each row.

    If you do the following:

    RowTimes = seconds(1:5)';
    RowTimes.Format = 'hh:mm:ss.SSS';
    

    Then the timestamps in TT.txt will be of the format 00:00:01.000, 00:00:02.000, etc, which is a valid duration format for reading back in without any additional inputs to readtimetable:

    It should also work without the milliseconds, or with other formats including days etc.

    TT2 =
    
      5×2 timetable
    
        RowTimes    Reading1    Reading2
        ________    ________    ________
    
        00:00:01        98        120   
        00:00:02      97.5        111   
        00:00:03      97.9        119   
        00:00:04      98.1        117   
        00:00:05      97.9        116