Why 'NaT' (class: datetime) does not work with find function?

5 ビュー (過去 30 日間)
Atanu
Atanu 2022 年 7 月 1 日
コメント済み: Star Strider 2022 年 7 月 1 日
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?

採用された回答

Star Strider
Star Strider 2022 年 7 月 1 日
Use the isnat function to test for it.
.
  2 件のコメント
Atanu
Atanu 2022 年 7 月 1 日
Thank you! It worked.
Star Strider
Star Strider 2022 年 7 月 1 日
As always, my pleasure!

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

その他の回答 (1 件)

Steven Lord
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 = 1×3
1 NaN 2
x == x
ans = 1×3 logical array
1 0 1
isequal(x, x)
ans = logical
0
y = [NaT datetime('today')]
y = 1×2 datetime array
NaT 01-Jul-2022
y == y
ans = 1×2 logical array
0 1
isequal(y, y)
ans = logical
0
You will need to identify the NaT values with isnat or ismissing.
isnat(y)
ans = 1×2 logical array
1 0
ismissing(y)
ans = 1×2 logical array
1 0
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)
ans = logical
1
  1 件のコメント
Atanu
Atanu 2022 年 7 月 1 日
Thanks for the explanation!

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

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by