How do I truncate a table based on a data window I'd like to use?

19 vues (au cours des 30 derniers jours)
Timothy
Timothy le 19 Août 2023
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.

Réponse acceptée

Voss
Voss le 19 Août 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)
T = 86400×3 table
TimeReceived TimeSent RecordType ______________________________ __________ __________ 26-Jun-2023 00:00:00.002600000 1.6878e+18 160 26-Jun-2023 00:00:01.002600000 1.6878e+18 160 26-Jun-2023 00:00:02.002600000 1.6878e+18 160 26-Jun-2023 00:00:03.002600000 1.6878e+18 160 26-Jun-2023 00:00:04.002600000 1.6878e+18 160 26-Jun-2023 00:00:05.002600000 1.6878e+18 160 26-Jun-2023 00:00:06.002600000 1.6878e+18 160 26-Jun-2023 00:00:07.002600000 1.6878e+18 160 26-Jun-2023 00:00:08.002600000 1.6878e+18 160 26-Jun-2023 00:00:09.002600000 1.6878e+18 160 26-Jun-2023 00:00:10.002600000 1.6878e+18 160 26-Jun-2023 00:00:11.002600000 1.6878e+18 160 26-Jun-2023 00:00:12.002600000 1.6878e+18 160 26-Jun-2023 00:00:13.002600000 1.6878e+18 160 26-Jun-2023 00:00:14.002600000 1.6878e+18 160 26-Jun-2023 00:00:15.002600000 1.6878e+18 160
% 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,:)
T = 25200×3 table
TimeReceived TimeSent RecordType ______________________________ __________ __________ 26-Jun-2023 08:00:00.002600000 1.6878e+18 160 26-Jun-2023 08:00:01.002600000 1.6878e+18 160 26-Jun-2023 08:00:02.002600000 1.6878e+18 160 26-Jun-2023 08:00:03.002600000 1.6878e+18 160 26-Jun-2023 08:00:04.002600000 1.6878e+18 160 26-Jun-2023 08:00:05.002600000 1.6878e+18 160 26-Jun-2023 08:00:06.002600000 1.6878e+18 160 26-Jun-2023 08:00:07.002600000 1.6878e+18 160 26-Jun-2023 08:00:08.002600000 1.6878e+18 160 26-Jun-2023 08:00:09.002600000 1.6878e+18 160 26-Jun-2023 08:00:10.002600000 1.6878e+18 160 26-Jun-2023 08:00:11.002600000 1.6878e+18 160 26-Jun-2023 08:00:12.002600000 1.6878e+18 160 26-Jun-2023 08:00:13.002600000 1.6878e+18 160 26-Jun-2023 08:00:14.002600000 1.6878e+18 160 26-Jun-2023 08:00:15.002600000 1.6878e+18 160
  2 commentaires
Timothy
Timothy le 19 Août 2023
Thanks!
Voss
Voss le 19 Août 2023
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (1)

Seth Furman
Seth Furman le 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)
tt = 86400×2 timetable
TimeReceived TimeSent RecordType ______________________________ __________ __________ 26-Jun-2023 00:00:00.002600000 1.6878e+18 160 26-Jun-2023 00:00:01.002600000 1.6878e+18 160 26-Jun-2023 00:00:02.002600000 1.6878e+18 160 26-Jun-2023 00:00:03.002600000 1.6878e+18 160 26-Jun-2023 00:00:04.002600000 1.6878e+18 160 26-Jun-2023 00:00:05.002600000 1.6878e+18 160 26-Jun-2023 00:00:06.002600000 1.6878e+18 160 26-Jun-2023 00:00:07.002600000 1.6878e+18 160 26-Jun-2023 00:00:08.002600000 1.6878e+18 160 26-Jun-2023 00:00:09.002600000 1.6878e+18 160 26-Jun-2023 00:00:10.002600000 1.6878e+18 160 26-Jun-2023 00:00:11.002600000 1.6878e+18 160 26-Jun-2023 00:00:12.002600000 1.6878e+18 160 26-Jun-2023 00:00:13.002600000 1.6878e+18 160 26-Jun-2023 00:00:14.002600000 1.6878e+18 160 26-Jun-2023 00:00:15.002600000 1.6878e+18 160
tr = timerange(datetime(2023,6,26,8,0,0),datetime(2023,6,26,15,0,0),"closed");
tt = tt(tr,:);
head(tt)
TimeReceived TimeSent RecordType ______________________________ __________ __________ 26-Jun-2023 08:00:00.002600000 1.6878e+18 160 26-Jun-2023 08:00:01.002600000 1.6878e+18 160 26-Jun-2023 08:00:02.002600000 1.6878e+18 160 26-Jun-2023 08:00:03.002600000 1.6878e+18 160 26-Jun-2023 08:00:04.002600000 1.6878e+18 160 26-Jun-2023 08:00:05.002600000 1.6878e+18 160 26-Jun-2023 08:00:06.002600000 1.6878e+18 160 26-Jun-2023 08:00:07.002600000 1.6878e+18 160
tail(tt)
TimeReceived TimeSent RecordType ______________________________ __________ __________ 26-Jun-2023 14:59:52.002600000 1.6878e+18 160 26-Jun-2023 14:59:53.002600000 1.6878e+18 160 26-Jun-2023 14:59:54.002600000 1.6878e+18 160 26-Jun-2023 14:59:55.002600000 1.6878e+18 160 26-Jun-2023 14:59:56.002600000 1.6878e+18 160 26-Jun-2023 14:59:57.002600000 1.6878e+18 160 26-Jun-2023 14:59:58.002600000 1.6878e+18 160 26-Jun-2023 14:59:59.002600000 1.6878e+18 160

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by