isequal command suddently stops working

1 view (last 30 days)
Robert Kaufmann
Robert Kaufmann on 19 Jan 2022
Commented: Stephen23 on 19 Jan 2022
I am running Matlab 2019a on a Mac that is running Sierra version 10.12.6 . Part of the program compares dates to organize a data set. The code uses isequal to check whether dates are equal. But today, the comparison no longer identifies dates that are equal. I verified this error by checking values from the command window
Match =
datetime
12/05/16
Observation(1261,1)
ans =
datetime
12/05/16
These two items are equal, but the isequal command does not recognize them as equal (it used to)
Match == Observation(1261,1)
ans =
logical
0
isequal(Match,Observation(1261,1))
ans =
logical
0
Here is one of the loops that does the comparison:
for I = 8454:rows ; % 8434 starts search in Nov 2016
Match = datetime(NYMEX_Dat(I,1), 'format', 'MM/dd/yy') ; %
for J = 1:Erows
if(isequal(Match,Observation(J,1)) == 1)
WTI(J,1) = WTI_Pr(I,1) ;% Assign price
end
end
end
  2 Comments
Stephen23
Stephen23 on 19 Jan 2022
Please upload your data in a mat file by clicking the paperclip button.
John D'Errico
John D'Errico on 19 Jan 2022
Edited: John D'Errico on 19 Jan 2022
Please learn to use comments, not answers to respond to a question for further information.
Answer by @Robert Kaufmann moved to a comment.
"Thanks you so much for your rapid response! I attach the code (Data_Prep.m) and the data files needed to run it. As recently as yesterday, the code ran fine. That it would not do the date comparisons on lines 21 and 30. I did get a warning last night about memory and I had the mac optimize storage, but that may be irrelevant information.
Again, I appreciate any help. I am at wits end. Thank you"

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 19 Jan 2022
So how different are the two datetime arrays you expected to be equal? Do they just look equal?
T = datetime('today')
T = datetime
19-Jan-2022
N = datetime('now', 'Format', T.Format) % N has time data but it doesn't show
N = datetime
19-Jan-2022
T == N % False
ans = logical
0
T and N are separated by many hours even though with the Format they're each using they appear to be the same.
hours(N - T)
ans = 18.6235
For datetime arrays, == requires the two datetime arrays to be exactly the same. Off by even a fraction of a second means no match. You probably want to compare with a tolerance.
N2 = N + seconds(1e-6);
N == N2 % false
ans = logical
0
abs(seconds(N-N2)) < 1 % within 1 second?
ans = logical
1
Or depending on what you're trying to do maybe calling discretize or interp1 would satisfy your needs?
  2 Comments
Robert Kaufmann
Robert Kaufmann on 19 Jan 2022
But I am only reading in data that has day, month, and year. Where would information beyond that come from? And why did it run earlier? I will try your suggestions....
Stephen23
Stephen23 on 19 Jan 2022
"Where would information beyond that come from?"
Default values for the other units. Ultimately the DATETIME object is just a fancy serial date number: changing what units you want displayed does not change the precision inherent in that serial date number.
"And why did it run earlier?"
Because you had different data values or different code.

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!