How do I truncate a table based on a data window I'd like to use?
23 views (last 30 days)
Show older comments
Hello,
I have a table with a DateTime group in the first column. It runs from midnight to midnight on a certain day. I would like to truncate the hours to between 8:00 AM and 3:00 PM, and discard all other data in the table. Is there a way to do this, or do I have to translate the date-time to something different to perform this operation? Thank you.
0 Comments
Accepted Answer
Voss
on 19 Aug 2023
% I construct a table similar to yours:
TimeReceived = datetime(2023,6,26,0,0,(0:60*60*24-1).',2.6,'Format','dd-MMM-yyyy HH:mm:ss.SSSSSSSSS');
N = numel(TimeReceived);
TimeSent = 1.6878e18*ones(N,1);
RecordType = 160*ones(N,1);
T = table(TimeReceived,TimeSent,RecordType)
% keep only rows of T where TimeReceived is between 8 AM and 3 PM inclusive
tod = timeofday(T.TimeReceived);
idx = tod >= hours(8) & tod <= hours(15);
T = T(idx,:)
More Answers (1)
Seth Furman
on 14 Sep 2023
Alternatively you can use timerange.
TimeReceived = datetime(2023,6,26,0,0,(0:60*60*24-1).',2.6,'Format','dd-MMM-yyyy HH:mm:ss.SSSSSSSSS');
N = numel(TimeReceived);
TimeSent = 1.6878e18*ones(N,1);
RecordType = 160*ones(N,1);
tt = timetable(TimeReceived,TimeSent,RecordType)
tr = timerange(datetime(2023,6,26,8,0,0),datetime(2023,6,26,15,0,0),"closed");
tt = tt(tr,:);
head(tt)
tail(tt)
0 Comments
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!