Synchronize two timetables with millisecond precision
15 views (last 30 days)
Show older comments
I'm trying to synchronize two timetables, and linearly interpolate the missing datapoints. I need the resulting, synchronized timetable to be accurate to the nearest 10th of a second, though. When I try to
I load in two tables with "readtable". The first table is straightforward. The second table "DataTable", has a time column with elapsed times, in milliseconds. Knowing the date and time of the start of the sample collection, I take this datetime, and add the datetime to the "time" column (which is in milliseconds),
DataTable.Time = datetime(YYYY, MM, DD, hh, mm, ss) + milliseconds(DataTable.Time);
This might be where the problem starts, as the new DataTable.Time is in a datetime format, but doesn't hold the time accuracy to the nearest millisecond.
Then I convert to timetable:
TimeTable2 = table2timetable(DataTable,'RowTimes','Time');
Then I synchronize the two tables. One method works fine, but there are repeated timestamps,
newTable = synchronize(TimeTable1, TimeTable2,'commonrange')
I really would like this to work:
newTable = synchronize(TimeTable1, TimeTable2,'commonrange', 'linear')
But because of the repeated timestamps, I get this error:
Error using timetable/synchronize (line 321)
Input timetables must contain unique row times when synchronizing using 'linear'.
Is there a way to use the "synchronize" function with row-times at a rate of 10 Hz?
0 Comments
Answers (3)
Walter Roberson
on 13 May 2018
DataTable.Time = datetime(YYYY, MM, DD, hh, mm, ss + DataTable.Time / 1000, 'Format', 'uuuu-MMM-dd HH:mm:ss.SSS');
Peter Perkins
on 14 May 2018
As Walter says, the ms thing is just a display format issue.
You say you are starting out with ms-resolutiuon data, and you want something that's regular at 10Hz. That sounds more like aggregation tyhan interpolation. You also say that there are missing data points, so that probably explains why you used interpolation.
You are going to need to reconcile the repeated timestamps. Having two values at one timestamp just isn't allowed for interpolation. You can run retime on one timetable, spacifying the existing time vector as the target, and using something like 'mean' or 'firstvalue'.
2 Comments
Peter Perkins
on 17 May 2018
From that error, it seems like you must have duplicate times in there somewhere. One quick way to find them would be something like
retime(tt,tt.Time,'count')
and then look for values bigger than 1. If that's not it, I think we'd have to see the data.
Tushar Agarwal
on 2 Aug 2018
Edited: Tushar Agarwal
on 2 Aug 2018
Hi Ford. I had a similar problem and indeed there were 5 duplicated time-stamps in my dataset (which I think is not easy to manually find in huge datasets). In fact, I didn't find the duplicates myself but just removed them using 2 simple lines of code.
% Find the unique set of times
uniqueTimes = unique(TimeTable2.Time or the name of the time-index column in newer MATLAB);
% retime all the data using this unique set
TimeTable2 = retime(TimeTable2,uniqueTimes,'mean');
This should solve your problem. The last option 'mean' basically replaces the duplicate-time data-entries with their means. You can use 'previous' to replace with the last duplicate-time entry or leave this argument out to replace with the first duplicate-time entry (by default). Hope this helps.
0 Comments
See Also
Categories
Find more on Timetables in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!