- TT is a timetable with datetime values stored in TT.Time.
- dateList is a vector of selected datetime values that indicate which days in TT should be accepted. All other rows of TT are removed.
How can I compare a datetime array with a timetable to delete all not existing days in it?
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Alexander Koch
il 20 Gen 2021
Modificato: Adam Danz
il 21 Gen 2021
I have a datetime array for some days of the year and a timetable with 24 hours a day (365 days, i.e. 8760 lines). I want to delete all lines in the timetable that are not in my datetime array. Which commands can I use? The timetable has values like temperature.
01/01/2000
01/02/2000
01/04/2000
01/06/2000
01/08/2000
01/20/2000
...
01/01/2000 00:00:00
01/01/2000 01:00:00
01/01/2000 02:00:00
01/01/2000 03:00:00
01/01/2000 04:00:00
01/01/2000 05:00:00
01/01/2000 06:00:00
01/01/2000 07:00:00
01/01/2000 08:00:00
01/01/2000 09:00:00
...
0 Commenti
Risposta accettata
Adam Danz
il 20 Gen 2021
Modificato: Adam Danz
il 20 Gen 2021
To match dates while ignoring time, use ismember() along with dateshift() to ignore time.
Demo:
% Create timetable
Time = datetime(2000,1,1,1,0,0)+days(0:30)'+hours(0:30)';
TT = timetable(Time, rand(size(Time)));
head(TT)
% List of accepted dates
dateList = datetime(2000,1,1)+days([0 1 3 5 7 19])'
% Identify and remove rows of TT with dates outside of dateList, ignoring time.
rmIdx = ~ismember(dateshift(TT.Time,'Start','Day'), dateshift(dateList,'Start','Day'));
TT(rmIdx,:) = []
12 Commenti
Adam Danz
il 21 Gen 2021
Modificato: Adam Danz
il 21 Gen 2021
@Alexander Koch look at the documentation for datestr datevec and datenum. Unfortunately their format notation is not consistent with datetime. For those functions months use lowercase m. But your data in the mat file are using datetime which uses uppercase M for month. It's a very unfortunate inconsistency that tricks a lot of people. But the lesson is always to refer to the documentation when you're having these kinds of problems.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Dates and Time in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!