Readtable not reading time as expected
이전 댓글 표시
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
Jon
2023년 11월 16일
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
2023년 11월 16일
채택된 답변
추가 답변 (1개)
Walter Roberson
2023년 11월 16일
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
Cris LaPierre
2023년 11월 16일
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
2023년 11월 16일
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
2023년 11월 17일
Peter Perkins
2023년 11월 17일
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.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!