How do I truncate a table based on a data window I'd like to use?
23 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Voss
2023-8-19
% 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,:)
更多回答(1 个)
Seth Furman
2023-9-14
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!