Separating timeseries into winter and summer months and removing night data

6 views (last 30 days)
I have a timetable matrix called as TT1 (680 x 4). I want to run a loop that does : if its a summer month then remove data from all cloumns from time = 22:00 to 5:00. else, if its a winter month remove data from all column from time = 18:00 to 8:00. How do I do that? This is what I have so far:
summerMon = 3:8;
winterMon = [1,2,9,10,11,12];
for i = 1:length(TT1)
if x = month(TT1.time(i) == summerMon)
%then here I want to write: remove this and this time for summer for whole matrix
else y = month(TT1.time(i) == winterMon)
%then here I want to write: remove this and this time for winter for whole matrix

Accepted Answer

Peter Perkins
Peter Perkins on 30 Apr 2020
This is straight-forward using logical subscripting. No loops needed:
>> t = datetime(2020,1,1) + hours(0:2:365*24)';
>> tt = timetable(rand(size(t)),'RowTimes',t);
>> summer = ismember(month(tt.Time),3:8);
>> tod = timeofday(tt.Time);
>> summerDayTime = summer & (hours(5) < tod & tod < hours(22));
>> winterDayTime = ~summer & (hours(8) < tod & tod < hours(18));
>> ttDaytime = tt(summerDayTime | winterDayTime,:);
>> ttDaytime(1:10,:)
ans =
10×1 timetable
Time Var1
____________________ ________
01-Jan-2020 10:00:00 0.47859
01-Jan-2020 12:00:00 0.88889
01-Jan-2020 14:00:00 0.55983
01-Jan-2020 16:00:00 0.24912
02-Jan-2020 10:00:00 0.64779
02-Jan-2020 12:00:00 0.95873
02-Jan-2020 14:00:00 0.51567
02-Jan-2020 16:00:00 0.012846
03-Jan-2020 10:00:00 0.75199
03-Jan-2020 12:00:00 0.035388
>> ttDaytime(501:510,:)
ans =
10×1 timetable
Time Var1
____________________ _______
02-Apr-2020 14:00:00 0.28218
02-Apr-2020 16:00:00 0.70203
02-Apr-2020 18:00:00 0.93884
02-Apr-2020 20:00:00 0.83015
03-Apr-2020 06:00:00 0.29995
03-Apr-2020 08:00:00 0.43387
03-Apr-2020 10:00:00 0.43982
03-Apr-2020 12:00:00 0.47928
03-Apr-2020 14:00:00 0.18956
03-Apr-2020 16:00:00 0.49738

More Answers (0)

Categories

Find more on Dates and Time 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!