function  y=gapfil(x)
% GAPFIL: fills NaN's in x by linear interpolation
% y=GAPFIL(x) locates data gaps in x denoted by NaN and fills
% them by linear interpolation. Assumes x is a column vector 
% or matrix.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 2/19/00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[M,N]=size(x);
y=x;
for n=1:N
   I=find(isnan(x(:,n)));
   J=find(isfinite(x(:,n)));
   index=[1:M]';
   K=length(I);
   xtab=x(J,n);
   ind=index(J);
   for k=1:K
      y(I(k),n)=interp1(ind,xtab,I(k));
   end
end
