Subset timetable based on datetime values
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Louise Wilson
am 17 Mär. 2020
Bearbeitet: Adam Danz
am 23 Mär. 2020
Hello, I have a timetable which has datetime values incrementing in ten minute intervals over a year, e.g.:
DateTime BoatCount PV
___________________ _________ __
29.05.2019 10:30:00 1 0
29.05.2019 10:40:00 0 0
29.05.2019 10:50:00 0 0
29.05.2019 11:00:00 0 0
29.05.2019 11:10:00 0 0
29.05.2019 11:20:00 0 0
29.05.2019 11:30:00 0 0
29.05.2019 11:40:00 0 0
I want to subset this data so I am only including values recorded in daylight hours e.g. 0600-2030. How do I subset the data by datetime values? Something like...
daylighthours=TT2(TT2.DateTime=='29/05/2019 06:00:00' : '29/05/2019 20:30:00')
but which would cycle through each day of the year
0 Kommentare
Akzeptierte Antwort
Adam Danz
am 17 Mär. 2020
Here's a demo that determines which rows of a timetable have times that are between two values.
% Create a demo timetable
TT = timetable((datetime('now') - minutes(0:248:60000))', rand(242,1));
% Get time-part (in duration format)
dur = TT.Time - dateshift(TT.Time, 'start', 'day');
% Define sunrise and sunset
sunrise = duration(6,30,0); % 6:00
sunset = duration(20,30,0); % 20:30
% Determine which times are between sunrise and sunset
idx = dur >= sunrise & dur <= sunset;
% Isolate rows of timetable that are during daylight
TT(idx,:)
Note: Sunrise and sunset are not fixed every day. A better approach would be to us a vector of sunrise and sunset times the same length as the datetime column and to determine whether each row is between those associated sunrise/sunset values.
3 Kommentare
Adam Danz
am 18 Mär. 2020
Bearbeitet: Adam Danz
am 23 Mär. 2020
Thanks, @Akira Agata
@Louise Wilson , sunrise and sunset times not only depend on time-of-year but also on location. You could either search for and download those values or you may be able to compute them.
Update, here's an algorithm to follow
Update 2, Loren has just posted a blog where this algorithm is demonstrated within a GIF image.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Dates and Time finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!