How to extract specific dates from a datetime table?
    19 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Ashfaq Ahmed
      
 el 19 de Abr. de 2023
  
    
    
    
    
    Respondida: Eric Sofen
    
 el 20 de Abr. de 2023
            Hi!
I have a datetime table (DATE1.mat) containing some dates from 1984 to 2022 at 10:00 am. I have another date timetable (DATE2.mat) that contains data from 2005 to 2019 at 10:00 am with temperature values. Can anyone please tell me how to find only the dates from DATE2.mat file that belong to DATE1.mat list?
Any feedback will be greatly appreciated! 
0 comentarios
Respuesta aceptada
  Les Beckham
      
 el 19 de Abr. de 2023
        whos('-file', 'DATE1.mat')
whos('-file', 'DATE2.mat')
load('DATE1.mat')
load('DATE2.mat')
whos
head(datet)
head(B)
found_date_idx = ismember(datet, B.Time); % logical indexes for dates in datet that are found in B
B_selected = B(found_date_idx, :) % extract the rows from B that match the found dates
0 comentarios
Más respuestas (2)
  Eric Sofen
    
 el 20 de Abr. de 2023
        If you know all the datetimes have 10:00:00 time components and you're just matching dates, timetable subscripting does that directly. Of course, if you're concerned about wanting non-exact matches (e.g. to match things that occur on the same date but not the same time), then the approach suggested by Adam is helpful. Other approaches for dealing with inexact matches may involve retime, timerange, or withtol. 
load DATE1.mat
load DATE2.mat
B(datet,:)
0 comentarios
  Adam Danz
    
      
 el 19 de Abr. de 2023
        Datetimes are rounded down to the start of the day using dateshift.  Then, ismember finds matches between the two sets of dates.
load DATE1.mat  % datet (vector)
load DATE2.mat  % B  (table)
[isDateMatch, idx] = ismember(dateshift(datet,'start','day'),dateshift(B.Time,'start','day'));
B.Time(idx(isDateMatch)) corresponds with datet(isDateMatch)
This line verifies that they are equal.  
isequal(dateshift(B.Time(idx(isDateMatch)),'start','day') , dateshift(datet(isDateMatch),'start','day'))
0 comentarios
Ver también
Categorías
				Más información sobre Dates and Time en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!