Filtering a table with datatime on dates
    13 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    Niels Hawinkel
 le 28 Juil 2021
  
    
    
    
    
    Réponse apportée : Scott MacKenzie
      
 le 28 Juil 2021
            
I have a table(=tmp) with column 2 (=Var2) consisting of Datetimes (Year-Month-Day-HH-MI-S). There are multiple days in this column and I want to filter out 1 day (i.e. 2019-10-01). I have tried the following:
Filter = tmp(tmp.Var2==datetime(2019,10,1,:,:,:),:);
I get the following error:
 Unrecognized function or variable 'datetime'.
I have also tried the following:
Filter = tmp(tmp.Var2==datetime(2019,10,1),:);
which results in an empty table.
Anybody knows what to do?
Thank you in advance.
0 commentaires
Réponse acceptée
  Cris LaPierre
    
      
 le 28 Juil 2021
        
      Modifié(e) : Cris LaPierre
    
      
 le 28 Juil 2021
  
      In the first case, you are not using correct syntax for MATLAB's datetime function (it doesn't accept colons), so MATLAB is assuming you must have your own function or variable that does, but it can't find it.
In the second case, the == is looking for an exact match (when you don't specify time, midnight is used). None of your datetimes is an exact match.
If you are not looking for an exact match, use conditional statements instead of ==.
rmvDate = tmp.Var2 >= datetime(12019,10,1,0,0,0) & tmp.Var2 < datetime(12019,10,2,0,0,0);
Filter = tmp(rmvDate,:);
0 commentaires
Plus de réponses (2)
  Rik
      
      
 le 28 Juil 2021
        There is probably a native way to do this, but you could also do it yourself:
ref=datetime(2019,10,1);
L=day(ref)==day(tmp.Var2) & month(ref)==month(tmp.Var2) & year(ref)==year(tmp.Var2);
Filter = tmp(L,:);
0 commentaires
  Scott MacKenzie
      
 le 28 Juil 2021
        To remove rows corresponding to 2019-10-01, this should work: 
d = datetime(2019,10,01);
removeLogical = isbetween(tmp.Var2, d, d+1);
tmp(removeLogical,:) = [];
0 commentaires
Voir également
Catégories
				En savoir plus sur Dates and Time dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!