search interval in datetime
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Luca Re
am 20 Mai 2024
Kommentiert: Luca Re
am 21 Mai 2024
hi, I have a datetime array of several many days. I need to serch interval
example:
TimeA=22:00
TimeB=03:00
search datetime>=timeA and datetime<=timeB (22:00 ..22:01..22:02. etc......23:59 ..00:00...00:01.etc... 2:58..2:59 3:00)
So I want without checking the day to have the indices of that range in the whole array
(the times in datetime are already ordered in a correct chronological way so the next time is always after the previous one)
0 Kommentare
Akzeptierte Antwort
Steven Lord
am 21 Mai 2024
T = datetime('today') + hours(24*rand(10, 1))
TOD = timeofday(T)
Then compare to duration objects representing the ends of the interval of interest. In this case because the interval includes midnight we need to ask if the timeofday is greater than the first end or less than the second end.
past2200 = TOD > duration(22, 0, 0)
before0300 = TOD < duration(3, 0, 0)
T(past2200 | before0300)
results = table(T, past2200, before0300, past2200 | before0300, ...
'VariableNames', ["times", "> 2200", "< 0300", "overnight"])
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Calendar 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!