How to add datetime to a column in a table

5 次查看(过去 30 天)
I have a table that looks like this:
startTime endTime A B
________________ ________________ _________________ _____________
09-12-2019 21:00 09-12-2019 22:00 10 2
09-12-2019 22:00 09-12-2019 23:00 1 0
09-13-2019 01:00 09-13-2019 02:00 2 0
09-13-2019 05:00 09-13-2019 06:00 1 0
09-13-2019 13:00 09-13-2019 14:00 8 91
09-13-2019 14:00 09-13-2019 15:00 36 190
09-13-2019 15:00 09-13-2019 16:00 6 18
I want to populate it for every hour from the min to max start time
To generate these hours I used t3=min(Table.startTime); t4=max(Table.endTime); t_tot=t3:hours(1):t4
How can I add the missing hours to the table and have a value of 0 for A and B for these hours?
  1 个评论
Siddharth Bhutiya
Siddharth Bhutiya 2019-10-2
Is endTime for each entry always startTime + 1 hour ? If that is the case then you can create a timetable with startTime, A and B and then use retime to resample your time stamps to be hourly. I would suggest using a timetable instead of a table when working with time-stamped data as that would give you access to useful functions specific to time related data.
data = timetable(startTime,A,B);
hourlyData = retime(data,"hourly","fillwithconstant","Constant",0)
ans =
19×2 timetable
startTime A B
____________________ __ ___
12-Sep-2019 21:00:00 10 2
12-Sep-2019 22:00:00 1 0
12-Sep-2019 23:00:00 0 0
13-Sep-2019 00:00:00 0 0
13-Sep-2019 01:00:00 2 0
13-Sep-2019 02:00:00 0 0
13-Sep-2019 03:00:00 0 0
13-Sep-2019 04:00:00 0 0
13-Sep-2019 05:00:00 1 0
13-Sep-2019 06:00:00 0 0
13-Sep-2019 07:00:00 0 0
13-Sep-2019 08:00:00 0 0
13-Sep-2019 09:00:00 0 0
13-Sep-2019 10:00:00 0 0
13-Sep-2019 11:00:00 0 0
13-Sep-2019 12:00:00 0 0
13-Sep-2019 13:00:00 8 91
13-Sep-2019 14:00:00 36 190
13-Sep-2019 15:00:00 6 18
% If you need the endTimes you can add them after this
hourlyData.endTime = hourlyData.startTime + hours(1);

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by