function data1=fill_nearest(data)
[T,N]=size(data);
%remove missing observations in data and replace with nearest neighbour
data1=[];
for j=1:N
    tmp=data(:,j); %jth column
    %check if first or last value is nan
    if isnan(tmp(1))
        chck=-1;
        i=1;
        while chck<0
            if isnan(tmp(i))
                i=i+1;
            else
                chck=1;
            end
        end
        tmp(1)=tmp(i);
    end
    if isnan(tmp(T))
        chck=-1;
        i=T;
        while chck<0
            if isnan(tmp(i))
                i=i-1;
            else
                chck=1;
            end
        end
        tmp(T)=tmp(i);
    end
    
    %// Index array for factor
x = 1:numel(tmp);

%// Indices of NaNs
t2 = find(~isnan(tmp)); 

%// Replace NaNs with the closest non-NaNs
tmp1 = interp1(x(t2),tmp(t2),x,'nearest');
data1=[data1 tmp1'];
end

