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

조회 수: 1 (최근 30일)
Ashfaq Ahmed
Ashfaq Ahmed 2023년 2월 15일
답변: dpb 2023년 2월 15일
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

채택된 답변

Kevin Holly
Kevin Holly 2023년 2월 15일
편집: Kevin Holly 2023년 2월 15일
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

추가 답변 (2개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 2월 15일
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
Ashfaq Ahmed
Ashfaq Ahmed 2023년 2월 15일
I liked this approach. It is meaningful. But Is there any way to get the interval time points? (i.e., 12:08)

댓글을 달려면 로그인하십시오.


dpb
dpb 2023년 2월 15일
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.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by