search interval in datetime
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Luca Re
el 20 de Mayo de 2024
Comentada: Luca Re
el 21 de Mayo de 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 comentarios
Respuesta aceptada
Steven Lord
el 21 de Mayo de 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"])
Más respuestas (0)
Ver también
Categorías
Más información sobre Calendar en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!