Find datetime in a vector

조회 수: 83 (최근 30일)
Tiago Dias
Tiago Dias 2019년 8월 16일
댓글: Tiago Dias 2019년 8월 16일
Hello,
I want to find the index of two times that belong on a colum with a bunch of timedates
t1 = datetime('10-09-2018 00:00:00','Format','dd-MM-uuuu HH:mm:ss')
t2 = datetime('28-09-2018 23:59:00','Format','dd-MM-uuuu HH:mm:ss')
idx_t1 = find(t_ref==t1)
idx_t2 = find(t_ref==t2)
idx_t1 should be 1020961 and idx_t2 1048320, but the results are empty
Thanks

채택된 답변

KSSV
KSSV 2019년 8월 16일
편집: KSSV 2019년 8월 16일
Use ismember
Or
idx = knnsearch(datenum(t_ref),datenum(t1)) ;
t_ref(idx)
t1
Or
dt = abs((datenum(t_ref)-datenum(t1))) ;
[val,idx] = min(dt) ;
t_ref(idx)
  댓글 수: 4
Guillaume
Guillaume 2019년 8월 16일
Any idea with the other two functions doesnt work?
Because, despite your statement, neither t1 nor t2 are present within t_ref. Note that the 'Format' property has no influence on the actual content of the datetime, so if they differ at the millisecond level, == will return false, even though you're not displaying the milliseconds.
If you want to get rid of the milliseconds:
t_ref.Second = floor(t_ref.Second); %or round if you prefer rounding to the nearest second
Tiago Dias
Tiago Dias 2019년 8월 16일
oh ok, I though he would ignore the miliseconds due to the format I specified.
thanks

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by