Logical result false when comparing 2 identical times
Show older comments
Hello, i want to compare times and something is wrong and i dont know why. Perhaps something is wrong in the Excel format of the data. but in matlab i think i placed the format of all the time equal.
clear;
clc;
close all;
tabela1 = readtable('galp.xlsx');
tabela2 = tabela1;
[n,m] = size(tabela1);
t_1 = datetime('01-01-2018 02:00','InputFormat','dd-MM-uuuu HH:mm');
t_2 = datetime('01-01-2018 06:00','InputFormat','dd-MM-uuuu HH:mm');
tempo_a_comparar = tabela2{121,1};
t_1
tempo_a_comparar
C = isequal(t_1,tempo_a_comparar)
6 Comments
Jos (10584)
on 7 Feb 2018
Edited: Jos (10584)
on 7 Feb 2018
If isequal returns false, they are, by definition, not equal.
I am not going to download your xls file, but you should be able to check for yourself using the debugging tools in matlab.
What do the two variables contain exactly?
Walter Roberson
on 7 Feb 2018
Subtract one from the other to see the time difference that MATLAB thinks exists.
Tiago Dias
on 8 Feb 2018
bandari shravya
on 27 Feb 2019
Getting error on comparing both datetimes
file2 = dir('matlabfiles');
filetest = struct2table(file2);
datemodified = datetime(filetest.date,'Format','dd/MMM/uuuu');
t1 = '08-Feb-2019 13:10:15';
to = datetime('26-Feb-2019','Format','dd/MMM/uuuu');
t2 = to ;
numfiles = length(file2);
for k = 1:numfiles
fprintf('%s ..',datemodified(k));
t2 = to ;
d = datemodified(k);
if isequal(d,t2)
fprintf('Done.\n');
else
fprintf('NOT .\n');
end
end
Walter Roberson
on 27 Feb 2019
When you called datetime(filetest.date,'Format', 'dd/MMM/uuuu') then it converts the entire time and records it, and sets the format so that what is displayed for the time is in the format dd/MMM/uuuu . 'Format' of a datetime has to do with display, not with how it is used in computing or how the input is parsed. The parameter to control how a character vector is parsed is 'InputFormat', or sometimes 'ConvertFrom'.
You can use ymd() to extract the year, month, and day of a datetime, or you can use dateshift(d,'start','day') to refer to the beginning of the day.
Accepted Answer
More Answers (0)
Categories
Find more on Dates and Time 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!