Extracting time valued between lower and upper bound

5 次查看(过去 30 天)
Hi Everyone,
I am trying to extract all the rows corresponding to lower and upper bound times. I have a big table (28125x31) and minimum and maximum times in table are 06:13:19 AM and 06:26:37 AM, respectively. I want to get data between t_min and t_max, whereas t_min = 06:16:44 AM and t_max = 06:17:05 AM. I am using following code
row = a([isbetween(a.time(:),t_min,t_max)],:);
where, a is table (28125x31), a.time is vector containing times as shown in attached excel sheet, t_min & t_max are described previously. While this bit of code, I am getting row table as 21871x31. When I check min and max time in row.time, they are 06:16:45 AM and 06:26:37 AM. The upper bound in row.time should be equal to 06:17:05 AM, but somehow code is giving me this value, which leads to inapporporaite size of table.
I have attached a .mat file (Data), containing a table file, which is named as "a".
Any help in figuring out the issue and how to get around it would be highly appreciated. Thank you.

采纳的回答

dpb
dpb 2022-9-13
>> tmp=a(1:10,[end-2:end]) % look at a sample of the pertinent data
ans =
10×3 table
datetime date time
___________________ ___________ ___________
2019-08-19 20:13:19 20-Aug-2019 06:13:19 AM
2019-08-19 20:13:19 20-Aug-2019 06:13:19 AM
2019-08-19 20:13:19 20-Aug-2019 06:13:19 AM
2019-08-19 20:13:19 20-Aug-2019 06:13:19 AM
2019-08-19 20:13:19 20-Aug-2019 06:13:19 AM
2019-08-19 20:13:20 20-Aug-2019 06:13:20 AM
2019-08-19 20:13:20 20-Aug-2019 06:13:20 AM
2019-08-19 20:13:19 20-Aug-2019 06:13:19 AM
2019-08-19 20:13:19 20-Aug-2019 06:13:19 AM
2019-08-19 20:13:19 20-Aug-2019 06:13:19 AM
>> tmp.time.Format='Default' % see what the time data really are...
tmp =
10×3 table
datetime date time
_______________________ ___________ ____________________
19-Aug-2019 08:13:19 PM 20-Aug-2019 20-Aug-2019 06:13:19
19-Aug-2019 08:13:19 PM 20-Aug-2019 20-Aug-2019 06:13:19
19-Aug-2019 08:13:19 PM 20-Aug-2019 20-Aug-2019 06:13:19
19-Aug-2019 08:13:19 PM 20-Aug-2019 20-Aug-2019 06:13:19
19-Aug-2019 08:13:19 PM 20-Aug-2019 20-Aug-2019 06:13:19
19-Aug-2019 08:13:20 PM 20-Aug-2019 20-Aug-2019 06:13:20
19-Aug-2019 08:13:20 PM 20-Aug-2019 20-Aug-2019 06:13:20
19-Aug-2019 08:13:19 PM 20-Aug-2019 20-Aug-2019 06:13:19
19-Aug-2019 08:13:19 PM 20-Aug-2019 20-Aug-2019 06:13:19
19-Aug-2019 08:13:19 PM 20-Aug-2019 20-Aug-2019 06:13:19
>>
NOTA BENE: despite it not being displayed, the time variable is still a datetime and a datetime CANNOT EXIST without a date as a time alone.
It's also interesting that the times seem to flip back and forth between 19 and 20 seconds rather than being monotonic....is this intended???
Look at the end of the table -- NOTA BENE there that there seems to be a problem on the last entry being same as first time element???
>> a(end-10+[1:10],[end-2:end])
ans =
10×3 table
datetime date time
___________________ ___________ ___________
2019-08-19 20:26:36 20-Aug-2019 06:26:36 AM
2019-08-19 20:26:37 20-Aug-2019 06:26:37 AM
2019-08-19 20:26:37 20-Aug-2019 06:26:37 AM
2019-08-19 20:26:37 20-Aug-2019 06:26:37 AM
2019-08-19 20:26:37 20-Aug-2019 06:26:37 AM
2019-08-19 20:26:37 20-Aug-2019 06:26:37 AM
2019-08-19 20:26:37 20-Aug-2019 06:26:37 AM
2019-08-19 20:26:37 20-Aug-2019 06:26:37 AM
2019-08-19 20:26:37 20-Aug-2019 06:26:37 AM
2019-08-19 20:13:19 20-Aug-2019 06:13:19 AM
>>
Now select between the times as stored; must use a datetime to match the actual values stored...
>> ix=isbetween(a.time,datetime(2019,8,20,6,16,44),datetime(2019,8,20,6,17,05));
>> sum(ix)
ans =
898
>>
To eliminate the date, use
>> tmp.TOD=timeofday(tmp.time)
tmp =
10×4 table
datetime date time TOD
_______________________ ___________ ____________________ ________
19-Aug-2019 08:13:19 PM 20-Aug-2019 20-Aug-2019 06:13:19 06:13:19
19-Aug-2019 08:13:19 PM 20-Aug-2019 20-Aug-2019 06:13:19 06:13:19
19-Aug-2019 08:13:19 PM 20-Aug-2019 20-Aug-2019 06:13:19 06:13:19
19-Aug-2019 08:13:19 PM 20-Aug-2019 20-Aug-2019 06:13:19 06:13:19
19-Aug-2019 08:13:19 PM 20-Aug-2019 20-Aug-2019 06:13:19 06:13:19
19-Aug-2019 08:13:20 PM 20-Aug-2019 20-Aug-2019 06:13:20 06:13:20
19-Aug-2019 08:13:20 PM 20-Aug-2019 20-Aug-2019 06:13:20 06:13:20
19-Aug-2019 08:13:19 PM 20-Aug-2019 20-Aug-2019 06:13:19 06:13:19
19-Aug-2019 08:13:19 PM 20-Aug-2019 20-Aug-2019 06:13:19 06:13:19
19-Aug-2019 08:13:19 PM 20-Aug-2019 20-Aug-2019 06:13:19 06:13:19
>> tmp.TOD
ans =
10×1 duration array
06:13:19
06:13:19
06:13:19
06:13:19
06:13:19
06:13:20
06:13:20
06:13:19
06:13:19
06:13:19
>>
which you see is now a duration of time since midnight of the given day. This works for a given day; if there are multiple days in a time vector, then it will have duplicate values for the same time-of-day of different days.
  6 个评论
dpb
dpb 2022-9-17
Great!
It's amazing how often trying to produce a small example that reproduces a problem will uncover the issue and actually solve the problem itself in doing so...

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by