select data from table according to date and time

Hallo,
I need your help for matlab.
I have a table with dates and times data for 7 days. I want to select records between 14:00 to 16:00 for each day?!
Thanks

2 件のコメント

Walter Roberson
Walter Roberson 2019 年 3 月 26 日
Are the times in a separate variable in the table, or do you have a single datetime column?
If the times are in a separate variable in the table, then what format are they in?
MOMO JOJO
MOMO JOJO 2019 年 3 月 27 日
Dear Walter,
The times are in the same table. Some of data are mentionned bellow. They are for 7 days
'08-Feb-2019 11:03:40'
'08-Feb-2019 11:08:40'
'08-Feb-2019 11:13:40'
'08-Feb-2019 11:18:40'
Thanks

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

回答 (1 件)

Akira Agata
Akira Agata 2019 年 3 月 27 日

1 投票

How about the following way?
% Create sample data
Time = sort(datetime(2019,3,24) + days(7)*rand(1000,1));
Data = rand(1000,1);
T1 = table(Time,Data);
% Select records (row index) between 14:00 to 16:00
idx = (T1.Time.Hour >= 14) & (T1.Time.Hour) < 16;
% Extract selected records
T2 = T1(idx,:);

8 件のコメント

MOMO JOJO
MOMO JOJO 2019 年 3 月 27 日
編集済み: MOMO JOJO 2019 年 3 月 27 日
Dear Akira,
It works :-) but if i want to select records between 14:00 to 16:30, what should I add to the prevois code,please.
I have put 16.5 but all records of 16:00 are extracted?
Thank you very much
Walter Roberson
Walter Roberson 2019 年 3 月 27 日
idx =(T1.Time.Hour >= 14) & ((T1.Time.Hour < 16) | (T1.Time.Hour == 16 & T1.Time.Minute <= 30));
Or you could do something like
temp = T1.Time.Hour * 60 + T1.Time.Minute;
idx = temp >= 14*60 & temp <= 16*60+30;
Akira Agata
Akira Agata 2019 年 3 月 27 日
Thank you, Walter-san !
MOMO JOJO
MOMO JOJO 2019 年 3 月 28 日
Thanks dears Walter-san & Akira Agata,
For your grand help.
Peter Perkins
Peter Perkins 2019 年 4 月 2 日
isbetween would probably make this more manageable:
>> isbetween(timeofday(datetime),'14:00:00','16:30:00')
ans =
logical
1
Aryan Ritwajeet Jha
Aryan Ritwajeet Jha 2021 年 4 月 7 日
isbetween command is much more convenient.
%Here, the 'time' column in the 'data' table stores the complete time for that instance
%in the format 'dd-MMM-yyyy hh:mm:ss'.
intervalStartTime = '09-Aug-2019 15:50:00';
intervalEndTime = '09-Aug-2019 16:00:00';
idx = isbetween(data.time,intervalStartTime,intervalEndTime);
selectedRows = data(idx,:); %Stores only the rows with the desired time instances
pruth
pruth 2022 年 2 月 21 日
hello,
what if i want to select minutes along with an hour. ? data between 14:30 and 15:30 ? how do i do it ?
Peter Perkins
Peter Perkins 2022 年 3 月 2 日
Presumably you would modify my answer from 2-Apr-2019.

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

カテゴリ

タグ

質問済み:

2019 年 3 月 26 日

コメント済み:

2022 年 3 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by