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

15 vues (au cours des 30 derniers jours)
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

Réponse acceptée

Chunru
Chunru le 24 Août 2022
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

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by