Logical result false when comparing 2 identical times

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 件のコメント

Jos (10584)
Jos (10584) 2018 年 2 月 7 日
編集済み: Jos (10584) 2018 年 2 月 7 日
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?
Jan
Jan 2018 年 2 月 7 日
編集済み: Jan 2018 年 2 月 8 日
@Tiago: Please explain, what you observe. Do you get e.g. error messages? Reading your question it does not get clear, why you assume, that there is something "wrong".
Walter Roberson
Walter Roberson 2018 年 2 月 7 日
Subtract one from the other to see the time difference that MATLAB thinks exists.
Tiago Dias
Tiago Dias 2018 年 2 月 8 日
@Jan Simon, thanks for your question, because now I saw that some are the month has a number "01" other is "Jan" and thats why it is false the return.
bandari shravya
bandari shravya 2019 年 2 月 27 日
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
Walter Roberson 2019 年 2 月 27 日
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.

サインインしてコメントする。

 採用された回答

Peter Perkins
Peter Perkins 2018 年 2 月 7 日

1 投票

The extension of the file you've posted does not match your code. The file you've posted appears to be a .xlsx file.
Add :ss.SSSSSSSSS to the end of all your formats. What you will see is this:
t_1 =
datetime
01/01/2018 02:00:00.000000000
tempo_a_comparar =
datetime
01/01/2018 02:00:00.000022949
ans =
-2.2949e-05
My initial reaction was to point out that Excel stores time in units of days since 1900. So in general, timestamps read from Excel are accurate only to about
>> eps(seconds(datetime - datetime(1900,0,0)))
ans =
4.7684e-07
seconds. But since the actual difference is 50 larger than that, I can only assume that what's in the .xlsx file is not what you think it is.

3 件のコメント

Tiago Dias
Tiago Dias 2018 年 2 月 8 日
@Peter Perkins There is anyway to delete in matlab the SSSSSSS?
when I input the file from excel, I just want Matlab to read hours minutes and seconds, I don't want Matlab to read SSSSS, is that possible?
Walter Roberson
Walter Roberson 2018 年 2 月 8 日
No, you need to read the SSSSS as well if you are dealing with text. However, you can
DateTimeArray = dateshift(DateTimeArray, 'start', 'second')
Tiago Dias
Tiago Dias 2018 年 2 月 8 日
yeah i found that as well in another question i asked, thanks a lot!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by