Extracting index of specified date from datetime array in dd:MM:yyyy:HH:mm:ss.SSS format

15 次查看(过去 30 天)
Hi All,
Getting frustrated as this should be seemingly simple to do. i have an array of datetime values in dd:MM:yyyy:HH:mm:ss.SSS format. I am trying to find the Index position of a specific time (03:04:2021:00:05:10.063)
TimeMatlab = [738249.003472956,738249.003530829,738249.003588699,738249.003646569,738249.003704439]
Time=datetime(TimeMatlab,'ConvertFrom','datenum','Format','dd:MM:yyyy:HH:mm:ss.SSS'); %Convert serial dates into dd:MM:yyyy:HH:mm:ss.SSS format
Time =
1×5 datetime array
03:04:2021:00:05:00.063 03:04:2021:00:05:05.063 03:04:2021:00:05:10.063 03:04:2021:00:05:15.063 03:04:2021:00:05:20.063
I then want to find the Index of 03:04:2021:00:05:10.063 in the datetime array Time (1,3).
The dates I need to serch for are in the dd:MM:yyyy:HH:mm:ss.SSS format hence I want to search for the index postion in Time rather than TimeMatlab if that makes sense.
Thanks

采纳的回答

Chunru
Chunru 2022-8-24
TimeMatlab = [738249.003472956,738249.003530829,738249.003588699,738249.003646569,738249.003704439];
Time=datetime(TimeMatlab,'ConvertFrom','datenum','Format','dd:MM:yyyy:HH:mm:ss.SSSSSSSS'); % SSSSSSS
Time = 1×5 datetime array
03:04:2021:00:05:00.06339965 03:04:2021:00:05:05.06362402 03:04:2021:00:05:10.06359692 03:04:2021:00:05:15.06355957 03:04:2021:00:05:20.06353247
T0=datetime("03:04:2021:00:05:10.063",'InputFormat','dd:MM:yyyy:HH:mm:ss.SSS')
T0 = datetime
03-Apr-2021 00:05:10
Time-T0
ans = 1×5 duration array
-00:00:09 -00:00:04 00:00:00 00:00:05 00:00:10
[~, idx] = min(abs(seconds(Time-T0))) % closest one to T0
idx = 3

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by