Cannot use find() inorder to find the index of a time in datetime array

13 ビュー (過去 30 日間)
Mathan
Mathan 2022 年 4 月 11 日
コメント済み: Mathan 2022 年 4 月 11 日
Hello,
I was trying to find the index of a particular time having datetime format (attached here as time.mat). I tried the usual find() inorder to find the index of an element but am constantly getting a 0×1 empty double column vector.
For example, I tried the following.
t=load('time.mat');
time=t.a; % a: [101×1 datetime]
index = find(time=='02-Dec-2014 07:05:25')
which gives me
0×1 empty double column vector.
Not sure why that is happening even though '02-Dec-2014 07:05:25' is present as the last element in the array.
Thank you for any help.

採用された回答

Matt J
Matt J 2022 年 4 月 11 日
編集済み: Matt J 2022 年 4 月 11 日
Because you have sub-second precision in "t.a",
t=load('time.mat');
t.a(end)
ans = datetime
02-Dec-2014 07:05:25
seconds( t.a(end) - datetime('02-Dec-2014 07:05:25'))
ans = 0.6015
  5 件のコメント
Matt J
Matt J 2022 年 4 月 11 日
t=load('time.mat');
tmin=min(t.a);
time=seconds(t.a-tmin);
N=numel(time);% a: [101×1 datetime]
index = interp1(time,1:N, seconds(datetime('02-Dec-2014 07:05:25')-tmin),'nearest')
Mathan
Mathan 2022 年 4 月 11 日
Thank you - it works!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by