Select multiple time ranges and variables in Timetable and create logical flag or filter

13 views (last 30 days)
Hello there,
A:
I would like to select multiple time ranges simultaneously in a Timetable.
I am trying to Flag these periods in a column of its own.
Currently, I have to do this twice, with two separate columns, as below, I would rather have one single flag:
Data{1}.Timestamp_Filter1 = Data{1}.Timestamp >= datetime("2020-01-01") & Data{1}.Timestamp <= datetime("2020-01-02");
Data{1}.Timestamp_Filter2 = Data{1}.Timestamp >= datetime("2022-01-01") & Data{1}.Timestamp <= datetime("2022-01-02");
I have already refered to:
B:
Similarly, but not for time ranges
Data{1}.angle_Filter3 = Data{idx}.angle >= 10 & Data{1}.angle <= 20;
Data{1}.angle_Filter4 = Data{idx}.angle >= 30 & Data{1}.angle <= 40;
How, can I do this once?
Support would be apprecaited. Thank you very much!

Accepted Answer

Seth Furman
Seth Furman on 28 Feb 2022
In this case we already know how to compute the rows we want using logical vectors, which we can use to index into the timetable directly.
TT = readtimetable("outages.csv")
TT = 1468×5 timetable
OutageTime Region Loss Customers RestorationTime Cause ________________ _____________ ______ __________ ________________ ___________________ 2002-02-01 12:18 {'SouthWest'} 458.98 1.8202e+06 2002-02-07 16:50 {'winter storm' } 2003-01-23 00:49 {'SouthEast'} 530.14 2.1204e+05 NaT {'winter storm' } 2003-02-07 21:15 {'SouthEast'} 289.4 1.4294e+05 2003-02-17 08:14 {'winter storm' } 2004-04-06 05:44 {'West' } 434.81 3.4037e+05 2004-04-06 06:10 {'equipment fault'} 2002-03-16 06:18 {'MidWest' } 186.44 2.1275e+05 2002-03-18 23:23 {'severe storm' } 2003-06-18 02:49 {'West' } 0 0 2003-06-18 10:54 {'attack' } 2004-06-20 14:39 {'West' } 231.29 NaN 2004-06-20 19:16 {'equipment fault'} 2002-06-06 19:28 {'West' } 311.86 NaN 2002-06-07 00:51 {'equipment fault'} 2003-07-16 16:23 {'NorthEast'} 239.93 49434 2003-07-17 01:12 {'fire' } 2004-09-27 11:09 {'MidWest' } 286.72 66104 2004-09-27 16:37 {'equipment fault'} 2004-09-05 17:48 {'SouthEast'} 73.387 36073 2004-09-05 20:46 {'equipment fault'} 2004-05-21 21:45 {'West' } 159.99 NaN 2004-05-22 04:23 {'equipment fault'} 2002-09-01 18:22 {'SouthEast'} 95.917 36759 2002-09-01 19:12 {'severe storm' } 2003-09-27 07:32 {'SouthEast'} NaN 3.5517e+05 2003-10-04 07:02 {'severe storm' } 2003-11-12 06:12 {'West' } 254.09 9.2429e+05 2003-11-17 02:04 {'winter storm' } 2004-09-18 05:54 {'NorthEast'} 0 0 NaT {'equipment fault'}
rowTimes = TT.Properties.RowTimes;
isJan = month(rowTimes) == 1;
is1stThrough3rd = 1 <= day(rowTimes) & day(rowTimes) <= 3;
is2000s = 2000 <= year(rowTimes) & year(rowTimes) <= 2009;
isJan1stThrough3rd2000s = isJan & is1stThrough3rd & is2000s;
TT(isJan1stThrough3rd2000s, :)
ans = 5×5 timetable
OutageTime Region Loss Customers RestorationTime Cause ________________ _____________ ______ __________ ________________ ____________________ 2005-01-02 03:26 {'MidWest' } 603.69 1.3631e+05 2005-01-14 05:15 {'winter storm' } 2006-01-02 04:55 {'NorthEast'} NaN 33134 2006-01-02 15:12 {'wind' } 2005-01-02 15:57 {'SouthEast'} 0 0 2005-01-02 16:29 {'energy emergency'} 2006-01-01 11:54 {'West' } 734.11 4.26e+06 2006-01-11 01:21 {'winter storm' } 2007-01-01 16:01 {'West' } 180.24 81389 2007-01-02 03:21 {'severe storm' }

More Answers (0)

Categories

Find more on Timetables in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!