MATLAB: Interpolation of 2D matrix using Interp2 to eliminate NaN

interpolationnan

I have a 310*400 matrix, that contain NAN values. I will like to interpolate the data to eliminate the NAN. After applying this code, I observed that the NAN is still retained. A sample of my code is here.
load('km100.dat'); % load the z column data
[x,y] = ndgrid(310,400); % arrange the data into grid
data_nan=reshape(km100,[],400); %


row_vect = 1:310; %
col_vect = 1:400; %
[X,Y] = meshgrid(col_vect, row_vect);
[Xq,Yq] = meshgrid((1:1:310),(1:1:400));
V = data_nan;
data_interp = interp2(X, Y, V, Xq', Yq', 'cubic');
How do I correct it? Thanks.

Best Answer

"How do I correct it? Thanks."
NaN data is missing data, so although you think that you have gridded data in reality you don't, because some of it is missing. Therefore you need to use some method that can interpolate non-gridded data:
To use: remove NaN values (giving vectors) and then interpolate at the requires grid points.
The easiest solution would be to download John D'Errico's excellent FEX submission, it might do what you want: