Gettind the indexes of table array with time and data according to datetime vector with time. Or how to syncronize datetime vector with table array
3 views (last 30 days)
Show older comments
Hi
I am very confused by datetime vector in Matlab and can not understand completely how I can solve my problem:
I have datetime vector with time:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/230218/image.png)
and I have a table array with time and corresponding data: ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/230219/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/230219/image.png)
I need to get a vector of indixes of the table array which correspond to time from datetime vector. For example to get smth like this (If you do manually):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/230220/image.png)
I tried to transform datetime array to a timetable and the use syncronize function but I didnot manage to do it. Tried to use find function but stil does not work.
Thank you!
These two arrays are attached
2 Comments
dpb
on 18 Jul 2019
I don't follow how the second vector is supposed to be related to the first???
What, specifically, are you trying to do?
Accepted Answer
dpb
on 18 Jul 2019
Edited: dpb
on 18 Jul 2019
OK, with the code to see what it was you were really trying to do it's easier...
First, there's an issue with your table that makes using the datetime more difficult...note the result of the following (firstly, I shortened the table variable name to just t for brevity):
> t.Time.Format='default';
>> t.Time(1:5)
ans =
5×1 datetime array
31-Dec-1899 11:48:00
31-Dec-1899 11:49:00
31-Dec-1899 11:50:00
31-Dec-1899 11:51:00
31-Dec-1899 11:52:00
>>
NB: the actual datetime value isn't the same date as the search date so nothing will match. This probably is a major reason you had no success with retime or other attempts to try to use the datetime values. Since inspection reveals all dates are the same day in the date column, I used it to create a new variable in the table that is the correct datetime for each as:
t.time=datetime(2019,7,9,hour(t.Time),minute(t.Time),second(t.Time));
To keep the two separate I used the lowercase time instead of Time; in reality I'd fix the Time variable in the table (or, better yet, create it initially with proper date and time).
Then, to get your result of the location of the closest prior time (I also named the lookup time series dt for less typing, I'm lazy...),
ix=interp1(t.time,1:height(t),dt,'previous')
ix =
207
207
207
206
206
205
205
205
204
204
204
204
204
205
205
205
206
206
207
207
207
>>
The nearest in absolute time differential generally returned the subsequent interval (but not always, there are a few such as the third location that are same):
>> interp1(t.time,1:height(t),dt,'nearest')
ans =
208
208
207
207
207
206
205
205
205
205
204
205
205
205
205
206
207
207
207
208
208
>>
You can choose whichever scheme you wish that best suits your needs...
3 Comments
dpb
on 19 Jul 2019
Or, import the time-only field from Excel as a duration instead of datetime. That would require an import options object to explcitly define the variable type
More Answers (1)
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!