"find" dont work for datetime variable ?
3 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
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')
dt2 = dt + seconds(0.1)
Is dt equal to itself? Of course.
y = dt == dt
Is dt equal to dt2? No. Even though they are displayed the same they differ by 0.1 seconds.
y2 = dt == dt2
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)
2 Comments
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.
See Also
Categories
Find more on Data Type Identification 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!