See if datetime is within range

3 views (last 30 days)
Joy
Joy on 25 Mar 2024
Commented: Joy on 25 Mar 2024
I have two datasets of datetime and numerical values where the data was collected at incongruous times. For example:
Dataset 1:
'3/25/2024 15:01:15' 1
'3/25/2024 15:01:31' 2
'3/25/2024 15:01:47' 0
'3/25/2024 15:02:03' 0
'3/25/2024 15:02:19' 0
'3/25/2024 15:02:35' 0
'3/25/2024 15:02:51' 1
'3/25/2024 15:03:07' 2
Dataset 2:
'3/25/2024 15:01:10' 10
'3/25/2024 15:01:26' 20
'3/25/2024 15:01:42' 30
'3/25/2024 15:01:58' 40
'3/25/2024 15:02:14' 50
'3/25/2024 15:02:30' 60
'3/25/2024 15:02:46' 70
I'd like to delete rows from dataset 1 and 2 that are in the same range of datetime. In this example, I'd like to delete the rows from dataset 1:
'3/25/2024 15:01:47' 0
'3/25/2024 15:02:03' 0
'3/25/2024 15:02:19' 0
'3/25/2024 15:02:35' 0
and therefore delete rows with the datetime in the same range from dataset 2:
'3/25/2024 15:01:58' 40
'3/25/2024 15:02:14' 50
'3/25/2024 15:02:30' 60
  2 Comments
Voss
Voss on 25 Mar 2024
You want to delete rows 3, 4, 5, and 6 from dataset 1, and also delete any rows from dataset 2 where the datetimes are within the range of the rows deleted from dataset 1. Is that right?
If so, why would rows 2 and 3 be deleted from dataset 2? Those are not in range (they are both before the first deleted dataset 1 datetime, '3/25/2024 15:01:47'). Only rows 4, 5, and 6 in dataset 2 are within the range '3/25/2024 15:01:47' to '3/25/2024 15:02:35'.
Joy
Joy on 25 Mar 2024
Pardon me, you're correct. It has been corrected.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 25 Mar 2024
I do not entirely understand what you want to do.
One approach —
D1 = {'3/25/2024 15:01:15' 1
'3/25/2024 15:01:31' 2
'3/25/2024 15:01:47' 0
'3/25/2024 15:02:03' 0
'3/25/2024 15:02:19' 0
'3/25/2024 15:02:35' 0
'3/25/2024 15:02:51' 1
'3/25/2024 15:03:07' 2};
D2 = {'3/25/2024 15:01:10' 10
'3/25/2024 15:01:26' 20
'3/25/2024 15:01:42' 30
'3/25/2024 15:01:58' 40
'3/25/2024 15:02:14' 50
'3/25/2024 15:02:30' 60
'3/25/2024 15:02:46' 70};
T1 = cell2table(D1);
T1.D11 = datetime(T1.D11)
T1 = 8x2 table
D11 D12 ____________________ ___ 25-Mar-2024 15:01:15 1 25-Mar-2024 15:01:31 2 25-Mar-2024 15:01:47 0 25-Mar-2024 15:02:03 0 25-Mar-2024 15:02:19 0 25-Mar-2024 15:02:35 0 25-Mar-2024 15:02:51 1 25-Mar-2024 15:03:07 2
T2 = cell2table(D2);
T2.D21 = datetime(T2.D21)
T2 = 7x2 table
D21 D22 ____________________ ___ 25-Mar-2024 15:01:10 10 25-Mar-2024 15:01:26 20 25-Mar-2024 15:01:42 30 25-Mar-2024 15:01:58 40 25-Mar-2024 15:02:14 50 25-Mar-2024 15:02:30 60 25-Mar-2024 15:02:46 70
Lv = T1.D12 == 0;
T1Q = T1.D11(Lv);
T2Q = T2.D21 >= T1Q(1) & T2.D21 <= T1Q(end); % Logical Vector
T2deleted = T2(T2Q,:) % Deleted Rows
T2deleted = 3x2 table
D21 D22 ____________________ ___ 25-Mar-2024 15:01:58 40 25-Mar-2024 15:02:14 50 25-Mar-2024 15:02:30 60
T2new = T2(~T2Q,:) % Retained Rows
T2new = 4x2 table
D21 D22 ____________________ ___ 25-Mar-2024 15:01:10 10 25-Mar-2024 15:01:26 20 25-Mar-2024 15:01:42 30 25-Mar-2024 15:02:46 70
.

More Answers (0)

Tags

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!