Clear Filters
Clear Filters

"find" dont work for datetime variable ?

6 views (last 30 days)
Timur
Timur on 26 Jul 2023
Commented: Stephen23 on 27 Jul 2023
I have two tables(main and logg), they look like "timetable" and have time format:yyyy-MM-dd HH:mm:ss.
Mission is simple, i must find last work day on "main" table, and update "main" from "logg-table" data. First column its datetime. At the first i make format "dd-MMM-yyyy". becouse i will copy data from all day.
then i will find "lastday" on the "logg-table". And here i stack becouse, in output nothing. I made time to str, nothing too.
where i make mistake.
table_lastday=readtable('C:\Users\Desktop\Datenlogger\Matlab\Datalogger.xlsx');
warning('off','all');
x_lastday=datetime(table_lastday.DatumUndUhrzeit); % read date from last workday
x_lastday=x_lastday(end);
x_lastday.Format = 'dd-MMM-yyyy';
x_history=datetime(KlimaLoggPro.Timestamp); %find date in history "Logg" KlimaLoggPro- its import text file
x_history.Format = 'dd-MMM-yyyy';
temp_time = find(x_history==x_lastday)
i hope you will not very angry on beginner.
Thanks for your comments.

Answers (1)

Steven Lord
Steven Lord on 26 Jul 2023
I suspect that find is working correctly and so is == when called on a datetime array, but == is not behaving as you expect. Let's take two datetime arrays that look identical in the default display format.
dt = datetime('now')
dt = datetime
26-Jul-2023 13:00:18
dt2 = dt + seconds(0.1)
dt2 = datetime
26-Jul-2023 13:00:18
Is dt equal to itself? Of course.
y = dt == dt
y = logical
1
Is dt equal to dt2? No. Even though they are displayed the same they differ by 0.1 seconds.
y2 = dt == dt2
y2 = logical
0
If your data is stored as two timetable arrays, I'd suggest using retime or synchronize to change the time basis of the timetable or perhaps using a timerange to index into it. Alternately, you could calculate the difference between your times and select those for which that difference is "small enough".
delta = seconds(dt2 - dt)
delta = 0.1000
  2 Comments
Timur
Timur on 27 Jul 2023
But how it ist work, when i extra use format with day "dd-MMM-yyyy" ? @Steven Lord you can be right(i have two tables with same data, and time rate 10m, but they not exsactly , they have diffrent timestmap maybe 1,5 minute one table late ). I will find same day only.
Stephen23
Stephen23 on 27 Jul 2023
"But how it ist work, when i extra use format with day "dd-MMM-yyyy""
Logical comparisons (e.g. EQ, LT, etc.) compare the datetime values themselves. Changing the display format makes absolutely no difference to the datetime value itself, just how it looks when it is displayed.

Sign in to comment.

Categories

Find more on Data Type Identification in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!