Unable to detect datetime or duration data using readtimetable with a seconds index

Here is a toy example of my problem:
% Create a timetable with a seconds index
data = timetable(seconds(1:3)', randn(3,1), randn(3,1))
writetimetable(data, "test.csv")
data_in = readtimetable("test.csv")
Output:
data =
3×2 timetable
Time Var1 Var2
_____ ________ _________
1 sec -0.10224 0.31286
2 sec -0.24145 -0.86488
3 sec 0.31921 -0.030051
Error using readtimetable (line 190)
Unable to detect datetime or duration data in file '/Users/username/test.csv' for row times.
The content of the CSV is as follows:
Time,Var1,Var2
1 sec,-0.225584402271252,0.0325574641649735
2 sec,1.11735613881447,0.552527021112224
3 sec,-1.08906429505224,1.10061021788087
For comparison, this works fine:
% Create a timetable with a regular datetime index
dt = datetime({'2015-12-18 08:00:00';'2015-12-18 08:01:00';'2015-12-18 08:02:00'});
data = timetable(dt, randn(3,1), randn(3,1));
writetimetable(data, "test.csv")
data_in = readtimetable("test.csv")

 채택된 답변

The issue appears to be that seconds get written in a format that MATLAB cannot read back in. The only supported input formats for durations are
  • 'dd:hh:mm:ss'
  • 'hh:mm:ss'
  • 'mm:ss'
  • 'hh:mm'
  • Any of the first three formats, with up to nine S characters to indicate fractional second digits, such as 'hh:mm:ss.SSSS'
The values are written with the units: 1 sec
For now, I would recommend creating and saving the timetable using one of the recognized formats.
% Create a timetable with a seconds index
data = timetable(duration(zeros(3,1),zeros(3,1),(1:3)'), randn(3,1), randn(3,1))
data = 3×2 timetable
Time Var1 Var2 ________ ________ ________ 00:00:01 -0.35574 0.35245 00:00:02 -0.60954 -0.22619 00:00:03 0.6993 1.9414
writetimetable(data, "test.csv");
data_in = readtimetable("test.csv")
data_in = 3×2 timetable
Time Var1 Var2 ________ ________ ________ 00:00:01 -0.35574 0.35245 00:00:02 -0.60954 -0.22619 00:00:03 0.6993 1.9414

댓글 수: 7

Thanks, this seems the best solution for now. Although this format (00:00:00) is not as nice-looking for plots of less than a few 100 seconds duration.
Also, I noticed that this simplification works:
duration(0,0,(1:3)')
Just modify your plot command to still plot seconds.
data = timetable(duration(0,0,[1 100 500]'), randn(3,1), randn(3,1))
data = 3×2 timetable
Time Var1 Var2 ________ ________ _________ 00:00:01 0.75423 1.4009 00:01:40 -0.51516 -0.83689 00:08:20 0.25414 -0.079847
plot(seconds(data.Time),data.Var1)
The problem I've got now with this solution is that durations of a fraction of a second are getting rounded in the csv file to the nearest second.
data = timetable(duration(0,0,(1.1:0.1:1.3)'), randn(3,1), randn(3,1));
writetimetable(data, "test.csv");
data_in = readtimetable("test.csv");
data_in.Time(1) == data_in.Time(3)
ans =
logical
1
How do I make writetimetable use a format like 'hh:mm:ss.SSS'? (Surprised it doesn't do this by default).
I'll admit I'm a little surprised as well, but confirmed the behavior with datetimes. It will write the data to file using the specified format. Any date/time information not visible in the displayed format will be lost.
Therefore, try setting your Time format before writing your table to csv.
data = timetable(duration(0,0,(1.1:0.1:1.3)'), randn(3,1), randn(3,1));
data.Time.Format = 'hh:mm:ss.SSS'
data = 3×2 timetable
Time Var1 Var2 ____________ ________ ________ 00:00:01.100 0.96684 1.3273 00:00:01.200 1.342 -1.0984 00:00:01.300 0.029173 -0.36432
writetimetable(data, "test.csv");
data_in = readtimetable("test.csv")
data_in = 3×2 timetable
Time Var1 Var2 ____________ ________ ________ 00:00:01.100 0.96684 1.3273 00:00:01.200 1.342 -1.0984 00:00:01.300 0.029173 -0.36432
Great, this solves the problem, thanks.
I just met with this same issue in R2022, update 2. Hopefully the incompatibility between writetimetable and readtimetable will be reslved in a future MATLAB update...
Issue is still present in R2023a Update 3

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Preprocessing에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

2021년 4월 13일

댓글:

2023년 9월 22일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by