Clear Filters
Clear Filters

Matching Time from 2 files/arrays

2 views (last 30 days)
There are basically 2 different ways I'm trying to solve the same problem. I have 2 time fields that are both cells (i.e. yyyy/MM/dd HH:mm:ss.ff) and I want to index/find where they match. Cell array 1 has 600 records, cell array 2 has 140 (almost a 0.25 frequency difference), so only 140 matches should actually come out.
Is there a way to match them based on not EXACT matches, but at least a threshold, say within +/- 0.5 seconds?
Also, the time is not the same in each array. Do I need to ensure the format matches before intersecting, or is there a function that doesn't care the string form?
I'm trying to do this via datastore, using the time TABLES, and also via normal matrices, where time is CELL ARRAYS.
  8 Comments
Bob Thompson
Bob Thompson on 1 Feb 2019
Edited: Bob Thompson on 1 Feb 2019
It is possible to specify the precision of the input to contain millisecond values, I would suggest reading over the documentation of datetime to find exactly which format you need.
With the values in datetime format you can generally just treat them as normal arrays, although conducting a subtraction of values will give a duration array.
I apologize if I'm not being very helpful, I don't normally work with full dates, just time values in seconds.
Bob Thompson
Bob Thompson on 1 Feb 2019
Doing some further searching it seems that using datetime limits arithmatic precision to seconds. If you are ok with having a 1 second precision then I would suggest you keep working with this format. But if you really need the ms precision then you will likely need to end up treating the values as strings, and using regexp to break it apart, or to convert the strings into numeric arrays, where each column represents a different time rank (year, month, day, ...).

Sign in to comment.

Accepted Answer

Bob Thompson
Bob Thompson on 1 Feb 2019
Ok, I think I found a more workable solution. First I would convert your cells to datetime, because I prefer working in regular numeric arrays than in cell arrays. Then I would convert the datetime back into a date vector format. This should generate an array of numbers which contains the ms precision you are looking for.
tmp = datetime([timearray{:}]);
time = datevec(tmp);
From there you can use normal array comparisons, using logic indexing, intersect, or other comparison commands.

More Answers (0)

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!