Clear Filters
Clear Filters

How to add datetime to a column in a table

5 views (last 30 days)
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 Comment
Siddharth Bhutiya
Siddharth Bhutiya on 2 Oct 2019
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);

Sign in to comment.

Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!