Select data from timetable according to date and time

Hello,
I want to plot some part of my data regarding smaller interval but I don't know how to create those interval since my time data is in datetime format.
I tried the isbetween function but I have this error message.
intervalStartTime = '12:37:48';
intervalEndTime = '12:37:58';
idx = isbetween (TTJosue.Time, intervalStartTime,intervalEndTime);
Error using datetime/isbetween>isbetweenUtil
All inputs must be datetime arrays or date/time character vectors or date/time strings.
Error in datetime/isbetween (line 59)
[aData,lData,uData] = isbetweenUtil(a,lowerLim,upperLim);
selectedRows = TTJosue(idx,:);
I don't understand where this error comes from because '12:37:48' and '12:37:58' are written in the same format as in my timetable, and I tried to formulate it differently.
Thanks,
Marine

3 commentaires

As the error message explains, they have to be datetime arrays to be used with datetime functions —
intervalStartTime = datetime('12:37:48', 'InputFormat','HH:mm:ss')
intervalStartTime = datetime
02-Aug-2022 12:37:48
intervalEndTime = datetime('12:37:58', 'InputFormat','HH:mm:ss')
intervalEndTime = datetime
02-Aug-2022 12:37:58
It might also be necessary to convert the table to a timetable to do what you want. See table2timetable for details.
.
Yes! I finally managed thanks to your comment. I just completely forgot that my serial date number (from which I exported the time data) was in dd/MM/yyyy HH:mm:ss format. That was why I couldn't select a new interval with the HH:mm:ss interval -_- …
Thank you again !
My pleasure!

Connectez-vous pour commenter.

Réponses (1)

If you want to determine if the time portion of a datetime falls in a certain window (regardless of the date portion) I recommend using the timeofday function to extract the time portion and calling isbetween on the resulting duration array and your time window data (also converted into duration arrays.)
First convert your time data into duration arrays.
intervalStartTime = '12:37:48';
intervalEndTime = '12:37:58';
dStart = duration(intervalStartTime)
dStart = duration
12:37:48
dEnd = duration(intervalEndTime)
dEnd = duration
12:37:58
Now let's get a sample datetime and extract its time portion.
n = datetime('now')
n = datetime
02-Aug-2022 13:23:47
dToCheck = timeofday(n)
dToCheck = duration
13:23:47
dToCheck is not in the interval [dStart, dEnd] and so isbetween returns false.
isbetween(dToCheck, dStart, dEnd)
ans = logical
0
Let's create another datetime whose time portion is in that interval.
dt = datetime('today') + duration('12:37:53')
dt = datetime
02-Aug-2022 12:37:53
dToCheck2 = timeofday(dt)
dToCheck2 = duration
12:37:53
Now isbetween will return true because of the day we built dt.
isbetween(dToCheck2, dStart, dEnd)
ans = logical
1

Catégories

En savoir plus sur Data Preprocessing dans Centre d'aide et File Exchange

Question posée :

le 2 Août 2022

Commenté :

le 2 Nov 2022

Community Treasure Hunt

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

Start Hunting!

Translated by