MATLAB: Linear interpolation in rows of matrix

interpolationMATLABmatrix

Hi!
I have vehicle speed measurement data in the following form (small example):
Distance (from the point of measurement, in metres): 1, 2, 3, 4, 5, 6, 7
Speed of vehicle1 at these points (in km/h): 50, 54, NaN, 62, 62, 64, NaN
Speed of vehicle2 at the points: 40, NaN, NaN, NaN, 48, 54, 60
Speed of vehicle3 at the points: NaN, 60, 62, 62, 66, NaN, NaN
Speed of vehicle 4 at the points: 44, 47, NaN, 55, NaN, NaN, 58
I would like to get a value instead of NaN with linear interpolation. BUT I dont want to interpolate if NaNs are the first/last elements of the measurement.
Needed result of example above: [50 54 58 62 62 64 NaN; 40 42 44 46 48 54 60; NaN 60 62 62 66 NaN NaN, 44 47 51 55 56 57 58].
Im absolutely a novice user of MATLAB, but I can not handle this problem in Excel, so I really need your help. How to insert these data (in a matrix form or how?), and how to get a good result with that I can work further (there are quite a lot vehicles in this database :S) ?
Thank you in advance
Gábor

Best Answer

The fillmissing function (R2016b and later) will do what you want:
D = [1, 2, 3, 4, 5, 6, 7];
V1 = [50, 54, NaN, 62, 62, 64, NaN];
V2 = [40, NaN, NaN, NaN, 48, 54, 6];
V3 = [NaN, 60, 62, 62, 66, NaN, NaN];
V4 = [44, 47, NaN, 55, NaN, NaN, 58];
Vs = [V1;V2;V3;V4];
Vout = fillmissing(Vs,'linear',2, 'EndValues','none')
producing:
Vout =
50 54 58 62 62 64 NaN
40 42 44 46 48 54 6
NaN 60 62 62 66 NaN NaN
44 47 51 55 56 57 58
As requested.