MATLAB: Row insertion and interpolation

interpolationrow insertion

Hi,
I have a dataset as follows
data = [0 135 140 500; 0.03 139 120 485; 0.06 133 127 489; 0.09 131 122 450; 0.15 160 149 503]
where column 1 is time in seconds, 2 is displacement along X, column 3 displacement along Y and column 4 displacement along Z. As you can see, I should have values every 0.03s. Sometimes however one row (or more!) is lost, and I would like to insert the missing info using a linear extrapolation for all columns.
Currently, I detect the missing rows by calculating tdiff
s = height(data);
t=data(:,1);
tdiff=zeros(s,1);
for i=2:s %start at cell 2 cell because 1 is first measurement
tdiff(i,1)=(t(i)-t(i-1));
end
and subsequently detect the location in time (locdroppedpose) and number (nlostpose) of lost rows (PoseT is how big the interval should be, in this case 0.03)
droppedpose=tdiff(tdiff>(PoseT));
locdroppedpose=t(tdiff>(PoseT));
nlostpose=droppedpose./(PoseT);
What I would like to do is insert into data the necessary number of rows (floor(nlostpose)) in the correct location (loclostpose).
I'm quite new to matlab and am not sure what the best ay to do this is. Any advice is appreciated.
Thank you!

Best Answer

Try this:
data = [0 135 140 500; 0.03 139 120 485; 0.06 133 127 489; 0.09 131 122 450; 0.15 160 149 503];
ti = min(data(:,1)) : 0.03 : max(data(:,1)); % Create New Time Vector
datai = [ti' interp1(data(:,1), data(:,2:end), ti, 'linear')] % Interpolate & Concatenate To Create New Matrix
datai =
0 135.0000 140.0000 500.0000
0.0300 139.0000 120.0000 485.0000
0.0600 133.0000 127.0000 489.0000
0.0900 131.0000 122.0000 450.0000
0.1200 145.5000 135.5000 476.5000
0.1500 160.0000 149.0000 503.0000
It uses the interp1 function with a time vector of the times you want to interpolate your data. (If you are doing signal processing, the Signal Processing Toolbox resample function is most appropriate.)