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

14 ビュー (過去 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
  1 件のコメント
Ashfaq Ahmed
Ashfaq Ahmed 2023 年 2 月 15 日
Perfect!! Thank you so much!!

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by