Readtable not reading time as expected
Show older comments
Hi there
I have run into a puzzling mishap by readtable. I'm running it as such::
T = readtable('file.data','FileType','text','NumHeaderLines',7);
Here is a cropped screenshot of the dataset I'm running it on (headerlines not shown):

I am specifically puzzled by the last column shown. This is the time of day. In the resulting table it comes back looking like this:

So it seems readtable has misinterpreted what is a time signature as a duration value. And that it is only able to determine this duration value when the third column (a nanosecond value) cycles back to zero.
Any ideas as to how I might resolve this problem?
Thanks in advance!
2 Comments
Jon
on 16 Nov 2023
Someone may be able to help you just by looking at your screenshot, but it would be helpful for me if you could attach your data file so I could try to import it myself and see what is going on.
Jakob Sievers
on 16 Nov 2023
Accepted Answer
More Answers (1)
Walter Roberson
on 16 Nov 2023
Use detectImportOptions() on the file. Then use setvartype() to set variable 8 to datetime instead of duration. Then use setvaropts() to set the InputFormat to 'HH:mm:ss:SSS'
Now readtable() the file passing in the modified options.
Variables 7 and 8 will now both be datetime.
Take
day_offset = TheTable{:,8} - dateshift(TheTable{:,8}, 'start', 'day');
TheTable.DateTime = TheTable{:,7} + day_offset;
Now TheTable.DateTime variable will hold the full-precision date and time together.
Yes, there are other ways, including passing a format to readtable() indicating what the datetype is for each column... but using %D and %T properly gets a bit messy. And in order for a time format to not automatically end at the space between date and time, you have to do the hack of telling the parser that whitespace is not a field delimiter... gets ugly.
4 Comments
Cris LaPierre
on 16 Nov 2023
opts = detectImportOptions('2022-08-29T191054_AIU-2371.data','FileType','text','NumHeaderLines',7);
opts = setvartype(opts,"Time","datetime");
opts = setvaropts(opts,"Time","InputFormat","HH:mm:ss:SSS");
data = readtable('2022-08-29T191054_AIU-2371.data',opts)
data.Time = timeofday(datetime(data.Time))
Jon
on 16 Nov 2023
You may also want to set the 'DatetimeFormat' for Time to be "HH:mm:ss.SSS" so it displays the milliseconds
opts = setvaropts(opts,"Time","InputFormat","HH:mm:ss:SSS",'DatetimeFormat','HH:mm:ss.SSS');
Jakob Sievers
on 17 Nov 2023
Peter Perkins
on 17 Nov 2023
Yes, the root cause here is that reading in duration text timestamps in the format hh:mm:ss:SSS is not currently supported. So Walter is correct, use datetime's more flexible parsing, and Chris is right, use timeofday to convert those to durations.
I have made note of adding support for this duration format, it's something we've had questions about before.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!