Delete rows from a table using a condition on datetimes
4 views (last 30 days)
Show older comments
Enrico Gambini
on 27 Jan 2022
Answered: Steven Lord
on 27 Jan 2022
Hello!
I'm working with a table T having two separate datetime columns containing dates ('M\d\yyyy'): let' say the first column is called dates1 and the second is called dates2. I want to find a way to keep ONLY the rows where the column dates2 is one day ahead of column dates1.
Can something like this work? I'm dealing with a huge set of data and I don't want to make mistakes
indexes=(T.dates1+1)~=T.dates2; %Positions to delete
T(indexes,:)=[]; %delete all the columns in those positions
Hope that the question is clear.
Thank you!
0 Comments
Accepted Answer
Steven Lord
on 27 Jan 2022
For the sake of humans reading your code I'd be more explicit and give that 1 some units.
rng default
t = datetime('today')
v1 = t + caldays(randi([-7 7], 1, 5))
v2 = v1 + caldays(randi([-1 1], 1, 5))
The fourth and fifth elements of v2 are 1 day after the corresponding elements of v1.
isOneDayLater = v2 == (v1 + caldays(1))
Why do I use caldays instead of just days?
daylightSavingsTimeStarts = datetime(2022, 3, 13, 'TimeZone', 'America/New_York')
daylightSavingsTimeStarts + days(1) % a 23 hour long day
daylightSavingsTimeStarts + caldays(1)
days(1) represents 24 hours. caldays(1) represents one calendar day, no matter how many hours that calendar day contains. That could be 23 hours, 24 hours, or 25 hours.
datetime(2022, 11, 6, 'TimeZone', 'America/New_York') + days(1) % DST ends
0 Comments
More Answers (0)
See Also
Categories
Find more on Calendar in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!