MATLAB: Error using interp1 on non-uniform vectors

errorgriddedinterpolantinterp1interpolateinterpolationnon uniform data

I'm using interp1 to interpolate some spatially non-uniform velocity data to a uniform spatial vector. My data looks something like this, with leading and trailing NaNs in the 2nd column (velocity) and real numbers in the 1st column (x-position):
data = [-5.13 NaN; -5.07 1.45; ...; 4.27 1.67; 5.03 NaN];
other_data = [-4.32 NaN; -4.14 1.57; ...; 4.25 1.89; 4.56 NaN];
I have several of these arrays of varied length with varied start and stop positions. My code grabs the smallest/largest x-values and uses them to build the grid for interpolation. For simplicity we'll say data contains the largest/smallest x values:
x_interp = [min(data(:,1)): 0.02 :max(data(:,1))].';
And the interp1 call looks like, where the first column of final_data is the x-values from x_interp:
final_data(:,i) = interp1(other_data(:,1),other_data(:,2),x_interp);
This call worked on one set of data, but not another and I'm having a hard time picking out what the difference in the data is. When I do this, I'm getting the following error message:
Error using griddedInterpolant
The grid vectors do not define a grid of points that match the given values.
Error in interp1 (line 166)
F = griddedInterpolant(X,V(:,1),method);
Any suggestions/ideas?

Best Answer

If the NaN values were at the leading and trailing (I assume at both ends) of your position-velocity matrix, and you are using the velocities to create your interpolation vector, you are likely extrapolating (or at least interp1 thinks so). See if changing the interp1 call to:
final_data(:,i) = interp1(other_data(:,1),other_data(:,2),x_interp, 'linear', 'extrap');
changes its behaviour. (You might want a different method than 'linear'. There are several to choose from, but if you extrapolate, you must specify a method.) Also, interp1 may not like the NaN values, so deleting the entries that have them may help get your code to work.
If you are still having problems with those changes, it could help if you uploaded a representative subset of the data that worked and the data that failed, along with the relevant part of the code you used in both situations. Did all of them have some NaN values in the velocity column?