Two equal datetime values showing as unequal when compared?
21 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Stephanie Arnold
el 5 de Nov. de 2019
Comentada: Alireza Washington
el 19 de Mzo. de 2024
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 comentario
Alireza Washington
el 19 de Mzo. de 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
Respuesta aceptada
Steven Lord
el 5 de Nov. de 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.
Más respuestas (2)
James Tursa
el 5 de Nov. de 2019
Editada: James Tursa
el 5 de Nov. de 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.
Sinan Islam
el 2 de Mayo de 2023
You can convert the dates to string before comparing them.
0 comentarios
Ver también
Categorías
Más información sobre Descriptive Statistics 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!