Matching Time from 2 files/arrays

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 Commenti

What about storing the data in a datetime format?
Tom W
Tom W il 1 Feb 2019
do you mean as serial time?
Tom W
Tom W il 1 Feb 2019
i'll try that - convert to serial time, run the intersection or intersecttol...didn't want to go that route because of precision and added runtime when using very big datasets
Not necessarily, I mostly mean that instead of storing the time as a cell, string, or table value, that you convert the class to a datetime format.
time = datetime(timearray{1});
If you give an example of how your data is stored I might be able to help in greater detail.
What exactly do you mean by 'serial' time? Perhaps we're talking past each other about the same thing.
Tom W
Tom W il 1 Feb 2019
Bob - so I converted the time array to a datetime.
How do I deal with sub-second precision in datetime?...I'm going to mess with this a little bit and see if datetimes are going to be the better way to go about this problem.
thx
Bob Thompson
Bob Thompson il 1 Feb 2019
Modificato: Bob Thompson il 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.
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, ...).

Accedi per commentare.

 Risposta accettata

Bob Thompson
Bob Thompson il 1 Feb 2019

1 voto

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.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by