isequal command suddently stops working
1 view (last 30 days)
Show older comments
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
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.
"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"
Answers (1)
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')
N = datetime('now', 'Format', T.Format) % N has time data but it doesn't show
T == N % False
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)
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
abs(seconds(N-N2)) < 1 % within 1 second?
Or depending on what you're trying to do maybe calling discretize or interp1 would satisfy your needs?
2 Comments
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.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!