how to select an interval of certain hours each day within a larger timetable using timerange
조회 수: 16 (최근 30일)
이전 댓글 표시
Hi, I have a timetable of minute by minute data running over multiple days. I wish to identify the times outside regular business hours (9 am to 5 pm) in order to (later) process that data differently from the rest. How do I do this using the timerange function? In other words, how do I select a timerange within each day, for every day, without selecting any full days? Thanks
댓글 수: 0
채택된 답변
Star Strider
2022년 3월 26일
The timerange function operates on timetable arrays, so to use it, the data must be in a timetable and the times must be duration arrays.
This uses table2timetable and duration array conversion —
DT = datetime('26-Mar-2022') + minutes(0:1440*2)'; % Two Days
Data = randn(numel(DT),3); % Create Other Values
T1 = [table(DT) array2table(Data)]
TT1 = table2timetable(T1);
TT1.DT = duration(hour(TT1.DT),minute(TT1.DT),second(TT1.DT))
BusinessHours = timerange(hours(9), hours(17), 'closed'); % 09:00 To 17:00
TT1_BusinessHours = TT1(BusinessHours,:)
OtherHours1 = timerange(hours(0),hours(9),'open'); % Midnight To 09:00
OtherHours2 = timerange(hours(17), hours(24), 'open'); % 17:00 To Midnight
TT1_OtherHours = [TT1(OtherHours1,:); TT1(OtherHours2,:)]
Using timerange is not as straightforward as I thought it would be, and I had to ‘fractionate’ it to get it to work.
% figure
% plot(TT1_BusinessHours.DT, TT1_BusinessHours.Data1)
% figure
% plot(TT1_OtherHours.DT, TT1_OtherHours.Data1)
.
댓글 수: 6
Star Strider
2022년 3월 26일
편집: Star Strider
2022년 3월 26일
In MATLAB, indices must be integers greater than 0 or logical values.
I note that you are using R2019a and I (and the online Run feature here) are using R2022a. There very well could be version differences, however that line:
TT1.DT = duration(hour(TT1.DT),minute(TT1.DT),second(TT1.DT))
simply converts ‘TT1.DT’ into a duration array by extracting the hour, minute, and second from the datetime array to create it. Looking at the R2019a documentation, the duration and hour (and I assume the others) do not appear to have changed. I am at a loss to explain the inconsistency, or the error.
It runs correctly in my code, and since I can’t reproduce it, I can’t troubleshoot it.
Check to see that the arguments to the duration function are the simple integers it requires for each argument. Then, see if the duration call works with the same vectors. That’s the only approach I can think of to determine what the problem is.
EDIT — (26 Mar 2022 at 21:50)
The only other possibility that I can think of is that you have an ‘hour’, ‘minute’, or ‘second’ array that is overshadowing the MATLAB functions by those names.
Run:
which hour -all
and do the same for each of the others. If the first entry in the output of that call is:
hour is a variable.
or something similar, that is the problem. The solution is to re-name the variable to something that makes sense in the context of the code, and does not overshadow any MATLAB functions.
.
추가 답변 (1개)
Simon Chan
2022년 3월 26일
Do you accept not using function timerange as follows?
T = readtable('date.txt')
T.Var1(hour(T.Var1)<9 | hour(T.Var1)>=17 & ~(hour(T.Var1)>=17 & minute(T.Var1)==0 & second(T.Var1)==0))
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!