MATLAB Answers

Introducing new rows (or columns) into an existing matrix with interpolated values to fill technically non-existent gaps.

13 views (last 30 days)
maffew on 4 Dec 2020
Commented: maffew on 4 Dec 2020
I have a dataset from a GPS unit that occasionally skips a second or two. I'd like to fill in the missing time gaps with the average of the previous and subsequent rows (well actually, that wouldn't work if there was a two second gap). Note: Each time I run this, the set of GPS data will be different and may or may not have these gaps in time.
For example, here is a snippet of what the matrix of interest looks like. The first column is a reference that mathes up with other data; After that it is YY MM DD HH MM SS. As you can see, the 49th second is missing.
GPS_time = [4791 20 11 8 23 32 47; 4792 20 11 8 23 32 48; 4794 20 11 8 23 32 50; 4795 20 11 8 23 32 51]
If you don't know but would like to help, I have two thoughts on how to approach this:
1. Create a complete reference from the first and last values of the reference column with no gaps to merge with the GPS_time matrix and using fillmissing to replace the missing values. I am trying to do this now, but am having trouble.
2. Give up and use the first and last rows of data from GPS_time to re-build the matrix from scratch. This doesn't sound fun because of the way time wraps around on itself before adding to the previous columns value.
I am using R2017a, btw.


Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 4 Dec 2020
Create a timetable() object and use retime()

  1 Comment

maffew on 4 Dec 2020
Thanks for your input.
Instead of using timetable, it seems more managable to use datetime and then datevec to recreate the matrix with the missing values. This was I can easily append the reference values.
t1 = datetime(2020,11,8,23,32,47);
t2 = datetime(2020,11,8,23,49,10);
t = t1:seconds(1):t2;
t = t';
t = datevec(t);
I would still like to know how to approach my original problem because it exists with data sets that are not time at all. But I still haven't been able to find a way to interpolate "missing" data. I think I would need to use my first approach to match a complete reference to force the dataset to have NaN values and then use fillmissing.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by