Extracting index of specified date from datetime array

조회 수: 120 (최근 30일)
Sreeraj T
Sreeraj T 2021년 5월 12일
댓글: Walter Roberson 2023년 3월 22일
Let us say that I have a datetime array which has dates that goes like these:
'2015-05-03 23:59:56'
'2015-05-03 23:59:56'
'2015-05-03 23:59:56'
'2015-05-03 23:59:56'
'2015-05-03 23:59:57'
'2015-05-03 23:59:57'
'2015-05-03 23:59:57'
'2015-05-03 23:59:57'
'2015-05-03 23:59:57'
'2015-05-03 23:59:57'
'2015-05-03 23:59:57'
'2015-05-03 23:59:57'
Now, I would like to know the indices of one certain element, say {'2015-05-03 23:59:57'}. How can I get that?
I tried with example given in https://in.mathworks.com/help/finance/datefind.html, but even for the example given there is not getting executed in my pc. It is giving an error ‘Undefined function 'datefind' for input arguments of type 'double'.’. I suspect that it may be due to not having the proper toolbox. The toolbox that I am having are:
Curve Fitting Toolbox, Parallel Computing Toolbox, Partial Differential Equation Toolbox, Signal Processing Toolbox and Symbolic Math Toolbox
Thanks in advance.

채택된 답변

Walter Roberson
Walter Roberson 2021년 5월 12일
S = datetime({'2015-05-03 23:59:56'
'2015-05-03 23:59:56'
'2015-05-03 23:59:56'
'2015-05-03 23:59:56'
'2015-05-03 23:59:57'
'2015-05-03 23:59:57'
'2015-05-03 23:59:57'
'2015-05-03 23:59:57'
'2015-05-03 23:59:57'
'2015-05-03 23:59:57'
'2015-05-03 23:59:57'
'2015-05-03 23:59:57'})
S = 12×1 datetime array
03-May-2015 23:59:56 03-May-2015 23:59:56 03-May-2015 23:59:56 03-May-2015 23:59:56 03-May-2015 23:59:57 03-May-2015 23:59:57 03-May-2015 23:59:57 03-May-2015 23:59:57 03-May-2015 23:59:57 03-May-2015 23:59:57 03-May-2015 23:59:57 03-May-2015 23:59:57
probe = datetime('2015-05-03 23:59:57')
probe = datetime
03-May-2015 23:59:57
find(S == probe)
ans = 8×1
5 6 7 8 9 10 11 12
  댓글 수: 3
Stephen23
Stephen23 2023년 3월 22일
@matteo franchi: it is clear that the date/time values do not exactly match each other. So you first need to decide if you want to find:
  • the single pair of closest date/time values?
  • all date/time values that are within a particular range/tolerance?
  • some other criteria...
Walter Roberson
Walter Roberson 2023년 3월 22일
Remember that when you use == that everything down to nanoseconds (or further) must be identical.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by