"find" dont work for datetime variable ?
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Steven Lord
2023-7-26
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 个评论
Stephen23
2023-7-27
"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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!