MATLAB: Resample multiple overlapping datetime to 1-minute (and 15-minutes) resolution

retimetabletimetable

Coming back again on resampling datetime….
Situation is I have many water pumps which are filled up in different time-frames. I have this table with multiple overlapping time and the total volume of individual pumps. Each row is basically a different water pump.
Column 1 (Start time), Column 2 (End time), Column 3 (Volume)
I want to sample the data to minute resolution (also 15 mins resolution) with the specified volume.
Example:
Pump1: 00:04:34 06:33:34 56
Pump2: 00:10:09 09:50:01 93
What I want it
00:04:00 56
00:05:00 56
….
00:10:00 56+93
00:11:00 56+93
….
06:34:00 93
As you can see, it sums up when there is overlapping time. Or else it is the only volume.
I have attached the actual table as well.
Thanks!

Best Answer

Let's start off with two simple timetable arrays, one per pump.
pump1 = timetable(datetime(["01-Jan-2018 00:14:09"; "01-Jan-2018 11:22:13"]), ...
[2.87; 2.87], 'VariableNames', {'Volume_pump1'})
pump2 = timetable(datetime(["01-Jan-2018 00:16:40"; "01-Jan-2018 14:15:10"]), ...
[4.709; 4.709], 'VariableNames', {'Volume_pump2'})
synchronize the two timetable arrays to a new one whose Times are regularly spaced (every 15 minutes), spanning the union of the Times for the two smaller timetable arrays. I'm using linear interpolation and no extrapolation, so the pump is only 'on' during the range spanned by the Times in its smaller timetable array from above.
P = synchronize(pump1, pump2, 'regular', 'linear', ...
'TimeStep', minutes(15), ...
'EndValues', 0)
Note that because I told synchronize to give regular spacing and the Times from pump1 and pump2 aren't multiples of 15 minutes, they're not included. You could construct a NEWTIMES vector (combine the Times together and use bounds to determine the earliest and latest times) and pass that into synchronize if you need those exact times to be included.
To get the total volume:
P2 = timetable(P.Time, sum(P.Variables, 2), 'VariableNames', {'totalVolume'})