how to select multiple time ranges in a timetable?

9 visualizzazioni (ultimi 30 giorni)
For this question, I asked the New Bing's chat, and it gave me an answer which shocked me (defeated by an AI) .
Yes, this was what I wanted. But this cannot work unfortunately, for the time range object does not support the operator ‘|’。
So what is the true answer ?

Risposte (1)

dpb
dpb il 9 Mag 2023
Modificato: dpb il 9 Mag 2023
That's pretty remarkable, indeed -- wonder how it managed to fake the output?
I thought first that just maybe the newest, online version had a new feature, but it fails here, still.
However, it does seem a remarkable oversight that the timerange object is so limited(*); instead you have to create a logical indexing array with which to do the logical operation. The following took an example from thetimerange page and just changed the times to be subsequent days instead...then use isbetween instead --
Time = datetime({'12/18/2015 08:00:00';'12/19/2015 10:00:0';'12/20/2015 12:00:00';...
'12/21/2015 14:00:00';'12/22/2015 16:00:00';'12/23/2015 18:00:00'});
Temp = [37.3;39.1;42.3;45.7;41.2;39.9];
Pressure = [30.1;30.03;29.9;29.8;30.0;29.9];
WindSpeed = [13.4;6.5;7.3;8.5;9.2;4.3];
WindDirection = categorical({'NW';'N';'NW';'NW';'NNW';'N'});
TT = timetable(Time,Temp,Pressure,WindSpeed,WindDirection)
TT = 6×4 timetable
Time Temp Pressure WindSpeed WindDirection ____________________ ____ ________ _________ _____________ 18-Dec-2015 08:00:00 37.3 30.1 13.4 NW 19-Dec-2015 10:00:00 39.1 30.03 6.5 N 20-Dec-2015 12:00:00 42.3 29.9 7.3 NW 21-Dec-2015 14:00:00 45.7 29.8 8.5 NW 22-Dec-2015 16:00:00 41.2 30 9.2 NNW 23-Dec-2015 18:00:00 39.9 29.9 4.3 N
%S = timerange('12/18/2015 08:00:00','12/18/2015 12:00:00')
S1 = isbetween(TT.Time,'12/18/2015 00:00:00','12/20/2015 12:00:00');
S2 = isbetween(TT.Time,'12/22/2015 00:00:00','12/22/2015 23:59:59');
S=S1 | S2
S = 6×1 logical array
1 1 1 0 1 0
TT(S,:)
ans = 4×4 timetable
Time Temp Pressure WindSpeed WindDirection ____________________ ____ ________ _________ _____________ 18-Dec-2015 08:00:00 37.3 30.1 13.4 NW 19-Dec-2015 10:00:00 39.1 30.03 6.5 N 20-Dec-2015 12:00:00 42.3 29.9 7.3 NW 22-Dec-2015 16:00:00 41.2 30 9.2 NNW
(*) ADDENDUM -- I suppose since the timerange object is independent of any given timetable, for it to be more than just one contiguous time range would add quite a bit of complexity to it and the method by which to use it would have to be able to build the composite indexing array from the given pieces.
However, it's worth a ping on the documentation I believe to address at least an example of selecting disjoint time sections; it's not mentioned anywhere in an example I'm aware of.
  1 Commento
Peter Perkins
Peter Perkins il 5 Giu 2023
Bing is wrong, dpb is right.
timerange only allows one interval. Depending on what your intervals are, it may be possible to use withtol, otherwise, dpb is correct: you currently need to use isbetween to create multiple logical vectors and OR those together.

Accedi per commentare.

Categorie

Scopri di più su Time Series Events in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by