Two equal datetime values showing as unequal when compared?

21 visualizzazioni (ultimi 30 giorni)
Hi,
I've been struggling with this issue for a few days and have searched through lots of Matlab documentation and am still struggling to find an answer.
Not sure of the protocol for providing code/files, but I have attached both in case and tried to simplify as much as possible.
TLDR: I have two datetime values that I know are equal, but when I try to compare them using '==' I get a logical of '0', meaning that they aren't being read as equal?
Long version: I have observed stream discharge data and simulated stream discharge data that are in tables in matlab. The observed data have gaps so the time steps are irregular, and only some times match up with the simulated data. Because of this, the two tables are also different lengths. I am trying to create a combined table where there is one time 't' variable (which is all of the occurrences where the observed and simulated data have the same times), and then a variable for the simulated discharge and a variable for the observed discharge. Since the tables are not the same length, I am trying to create the combined data by deleting any rows before or after the first and last matching time (this could be a problem though if there is missing data in between the start and end).
I used this code for another model previously and it worked completely fine, but I am now getting errors when I try to generate the combined data table. I decided to test to see if the values were reading as equal, so I tested the first matching time value between the two data sets and found that Matlab did not read them as equal (see below).
sim.t(25)
obs.t(46)
equaltest = sim.t(25)==obs.t(46)
ans =
datetime
03/24/17 09:05:00
ans =
datetime
03/24/17 09:05:00
equaltest =
logical
0
I can't tell if the issue with creating the combined data set is in my methods and code or if the issue is related to the datetime values not showing as equal when they really are?
Again, apologies that the question is a bit long- I wanted to make sure I provided enough info to help out whoever is kind enough to help answer my question!
  1 Commento
Alireza Washington
Alireza Washington il 19 Mar 2024
hey stephanie can i ask you share your purchase code time seies prediction with bayesian to my gmail:alirezarohanian4@gamail.com ill add your name to my paper for thanks

Accedi per commentare.

Risposta accettata

Steven Lord
Steven Lord il 5 Nov 2019
One thing you can do is check if they're "close enough":
s = datetime(2017,3,24,9,5,0.1);
t = datetime(2017,3,24,9,5,0.2);
seconds(s-t) % not zero, but small
abs(seconds(s-t)) <= 1 % within 1 second is "close enough"
But from your description of the problem you're trying to solve I recommend storing your data as two timetable arrays instead of two table arrays with a datetime variable inside. The table2timetable function will help you perform this conversion.
If you store your data as timetable arrays you can synchronize them to a common time basis, using various methods to fill, interpolate, or aggregate the data.
  1 Commento
Stephanie Arnold
Stephanie Arnold il 7 Nov 2019
I had tried using a timetable before using table2timetable, but I did not know about the synchronize option! I will give that a try, and if that doesn't work, I think that your "close enough" suggestion should definitely help. Thank you!

Accedi per commentare.

Più risposte (2)

James Tursa
James Tursa il 5 Nov 2019
Modificato: James Tursa il 5 Nov 2019
This is often just a display issue. E.g.,
>> s = datetime(2017,3,24,9,5,0.1)
s =
24-Mar-2017 09:05:00
>> t = datetime(2017,3,24,9,5,0.2)
t =
24-Mar-2017 09:05:00
>> s == t
ans =
0
Both s and t display the same, but they are not equal.
  1 Commento
Stephanie Arnold
Stephanie Arnold il 7 Nov 2019
You're right! I checked the longer numeric value and realized that they are, for my purposes, the same time but their values are not technically the same. Thank you!

Accedi per commentare.


Sinan Islam
Sinan Islam il 2 Mag 2023
You can convert the dates to string before comparing them.

Categorie

Scopri di più su Descriptive Statistics in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by