Why 'NaT' (class: datetime) does not work with find function?
5 次查看(过去 30 天)
显示 更早的评论
I have a table part of which is attached. The enries in the date column are as datetime. I am trying to get the id where the entry for date is NaT. When I use find function for regular dates such as,
idx = ans.id(find(ans.date == '19-Apr-2022'));
I get an output. But the same does not work for NaT.
idx = ans.id(find(ans.date == 'NaT'));
It gives me empty array. What might be the problem?
0 个评论
采纳的回答
更多回答(1 个)
Steven Lord
2022-7-1
The reason why isnat works and your == call did not is because NaT is like NaN -- it is not equal to anything, not even another NaT or NaN. It is not even equal to itself.
x = [1 NaN 2]
x == x
isequal(x, x)
y = [NaT datetime('today')]
y == y
isequal(y, y)
You will need to identify the NaT values with isnat or ismissing.
isnat(y)
ismissing(y)
Alternately you could use isequaln if you want to detect if two arrays (potentially containing NaT) are equal. isequaln behaves like isequal except it considers missing values equal to missing values.
isequaln(y, y)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!