How can I extract a specific time for a "datetime" table?

3 views (last 30 days)
Dear coders,
I have a simple question but I am confused how to code it. I have this datetime table (attached with the question: RESULT.mat) and I want to keep only those rows that contains time between 2:00-3:00 am and 12:00-13:00 pm. As shown in this picture -
The final datetime table should contain only these rows with the desired HourSeries. Any feedback from you will be highly appreciated! <3

Accepted Answer

Kevin Holly
Kevin Holly on 15 Feb 2023
Edited: Kevin Holly on 15 Feb 2023
load('RESULT.mat')
result
result = 16282×3 table
TimeSeries HourSeries HeightSeries ___________ __________ ____________ 10-Jan-2004 {'00:00'} 0.707 10-Jan-2004 {'01:00'} 0.997 10-Jan-2004 {'02:00'} 1.171 10-Jan-2004 {'03:00'} 1.134 10-Jan-2004 {'04:00'} 0.893 10-Jan-2004 {'05:00'} 0.557 10-Jan-2004 {'06:00'} 0.233 10-Jan-2004 {'07:00'} 0 10-Jan-2004 {'07:30'} -0.0215 10-Jan-2004 {'08:00'} -0.043 10-Jan-2004 {'09:00'} 0.116 10-Jan-2004 {'10:00'} 0.346 10-Jan-2004 {'11:00'} 0.562 10-Jan-2004 {'12:00'} 0.808 10-Jan-2004 {'13:00'} 1.084 10-Jan-2004 {'14:00'} 1.282
result.HourSeries = datetime(result.HourSeries,"Format","HH:mm")
result = 16282×3 table
TimeSeries HourSeries HeightSeries ___________ __________ ____________ 10-Jan-2004 00:00 0.707 10-Jan-2004 01:00 0.997 10-Jan-2004 02:00 1.171 10-Jan-2004 03:00 1.134 10-Jan-2004 04:00 0.893 10-Jan-2004 05:00 0.557 10-Jan-2004 06:00 0.233 10-Jan-2004 07:00 0 10-Jan-2004 07:30 -0.0215 10-Jan-2004 08:00 -0.043 10-Jan-2004 09:00 0.116 10-Jan-2004 10:00 0.346 10-Jan-2004 11:00 0.562 10-Jan-2004 12:00 0.808 10-Jan-2004 13:00 1.084 10-Jan-2004 14:00 1.282
index = hour(result.HourSeries)==12|hour(result.HourSeries)==2;
result(index,:)
ans = 1305×3 table
TimeSeries HourSeries HeightSeries ___________ __________ ____________ 10-Jan-2004 02:00 1.171 10-Jan-2004 12:00 0.808 26-Jan-2004 02:00 0.868 26-Jan-2004 12:00 0.319 11-Feb-2004 02:00 0.859 11-Feb-2004 12:00 0.354 19-Feb-2004 02:00 0.697 19-Feb-2004 12:00 1.56 19-Feb-2004 12:18 1.5045 27-Feb-2004 02:00 0.611 27-Feb-2004 12:00 0.307 14-Mar-2004 02:00 0.425 14-Mar-2004 12:00 0.204 22-Mar-2004 02:00 1.493 22-Mar-2004 12:00 0.958 07-Apr-2004 02:00 1.693

More Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 15 Feb 2023
Here is one partial answer that finds the data pointsat specific times, i.e. 02:00, 03:00, 12:00, 13:00 and overlooks other time points such as s12:08, for example:
R =load('RESULT.mat').result;
IDX1 = find(ismember(R.HourSeries, '02:00'));
IDX2 = find(ismember(R.HourSeries, '03:00'));
IDX3 = find(ismember(R.HourSeries, '12:00'));
IDX4 = find(ismember(R.HourSeries, '13:00'));
Rs = R([IDX1,IDX2, IDX3, IDX4, IDX4],:); % Selected
whos R Rs
Name Size Bytes Class Attributes R 16282x3 2118115 table Rs 2960x3 386255 table
  1 Comment
Ashfaq Ahmed
Ashfaq Ahmed on 15 Feb 2023
I liked this approach. It is meaningful. But Is there any way to get the interval time points? (i.e., 12:08)

Sign in to comment.


dpb
dpb on 15 Feb 2023
Convert to datetime and use <isbetween>
result.HourSeries=duration(result.HourSeries,'InputFormat','hh:mm');
ix=isbetween(result.HourSeries,hours(2),hours(4))|isbetween(result.HourSeries,hours(12),hours(13));
result=result(ix,:);
Keeps only the selected entries; you lose the rest and would have to reload the .mat file if need any other data. Doing the conversion and reSAVEing first would probably be the smart thing; then save that step going forward.

Community Treasure Hunt

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

Start Hunting!