MATLAB: Split values for a time-frame to every 15 minutes

split datatable

Hello all,
Firstly, apologies if the title is wrong. So, feel free to edit it. And, here's my situation. I have a certain values for a time-frame, like (table format) attached as pic:
Untitled.png
I want to re-sample the data every 15 minutes and the corresponding values. It should be such that when I sum the intervals, I should get the Volume for the time-frame.
Thanks

Best Answer

This should work.
First a function to operate on each row of your table:
function spreadtable = spreadvolume(dstart, dend, volume, dt)
%resample the interval [dstart, dend] into smaller intervals of duration dt and spread volume over these intervals
%note that the last interval may be shorter if dend-dstart is not a multiple of dt
%returns a table with 3 variables {'Start', 'End', 'Volume'} and as many rows as necessary
starts = (dstart:dt:dend)';
if starts(end) == dend %don't want [dend, dend] as the last interval.
starts(end) = [];
end
ends = [starts(2:end); dend];
dts = seconds(ends - starts);
volumes = volume / sum(dts) .* dts;
spreadtable = table(starts, ends, volumes, 'VariableNames', {'Start', 'End', 'Volume'});
end
Then apply to your table:
demotable = [array2table(datetime({'01-Jan-2018 13:11:02', '02-Jan-2018 12:15:40'; '01-Jan-2018 13:42:13', '02-Jan-2018 09:00:12'; '01-Jan-2018 13:42:18', '01-Jan-2018 14:02:33'}), 'VariableNames', {'Start', 'End'}), table([6.7; 19.25; 2.25], 'VariableNames', {'Volume'})]
resampled = rowfun(@(s, e, v) spreadvolume(s, e, v, minutes(15)), demotable, 'OutputFormat', 'cell');
resampled = vertcat(resampled{:})