timerange subscript within a timetable
조회 수: 2 (최근 30일)
이전 댓글 표시
End goal:
timeTableTT14to22 = timeTableTT(hours(14) <= timeofday(timeTableTT.Date) & timeofday(timeTableTT.Date) <= hours(22), :);
is there a bettwe way to do this ... using a timerange such as:
range14to22 = timerange(hours(14), hours(22), 'closed');
Essentially, having a multi-day / multi-year table with the Date column as a 10 minute interval datetime such as this:
2010-01-05 18:00
2010-01-05 18:10
2010-01-05 18:20
2010-01-05 18:30
2010-01-05 18:40
2010-01-05 18:50
2010-01-05 19:00
2010-01-05 19:10
2010-01-05 19:20
2010-01-05 19:30
How do you extract a daily timerange from such a table in a more elegant way, than the above? (say, you want a timetable with all the lines falling between 14 and 22 included).
And how do you marry this with other filters, such as - exclude bank holidays and sundays, for example, in an elegant and succint way?
Another example, extract year 2020:
timeTableTT(year(timeTableTT.Date) == 2020, :)
or this:
timeTableTT2020FirstHalf = timeTableTT2020(isbetween(timeTableTTTT2020.Date, datetime('2020-01-01'), datetime('2020-06-30'), 'closed'), :);
just feels awkwardly long and unnatural ... am I missing a better way to do subscripts like this?
댓글 수: 0
채택된 답변
Tejas
2024년 4월 24일
Hello Andy,
It seems you are looking for some assistance on how to effectively use the ‘timerange’ function to fetch data.
The ‘timerange’ function is designed to filter data by allowing us to specify a start time and an end time. According to the documentation, if the values in the timetable are datetime values, then both the start and end times should be datetime values as well. Conversely, if the values are of duration type (i.e. Time), then the start and end times must be duration values.
For applying the ‘timerange’ function to filter rows for duration values specifically between 14 hours and 22 hours, here is a helpful workaround. Split the datetime column into two separate columns, with the first column dedicated to time and the second to date. With this setup, the ‘timerange’ function can be effectively used to extract rows with duration values falling between 14 hours and 22 hours. The following code snippet illustrates how this can be achieved:
range14to22 = timerange(hours(14), hours(22), 'closed');
timeTableTT14to22 = timeTableTT(range14to22, :);
Similarly, if the first column consists of datetime values, the syntax below can be employed to fetch rows that contain data from the first half of a specific year.
halfYrRange = timerange('2020-01-01','2020-06-30','closed');
timeTableTT2020FirstHalf = timeTableTT(halfYrRange, :);
Based on my understanding, the ‘timerange’ function does not directly provide the option to apply additional filters. As a result, to exclude rows representing bank holidays and Sundays, it will be necessary to identify the indexes of these specific rows first and then filter them from the table.
Here is the output from a sample dataset that I created. The entries in my time column are spaced 30 minutes apart, and I have filtered the data to include only the entries between 2 hours to 3 hours.
For a more comprehensive understanding of the ‘timerange’ function, please consult the following documentation: https://www.mathworks.com/help/matlab/ref/timerange.html .
Hope it help!
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Calendar에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!