r_fill_data.m 1.27 KB
function data_f = r_fill_data( data )
% R_FILL_DATA - Replaces nan values by the next or previous value, depending
% on the position of the nan value. 
%
% Syntax:  data_f = r_fill_data(data)
%
% Inputs:
%    data     - Data to fill
%
% Outputs:
%    data_f   - Filled data 
%
% Example:
%    data_f = r_fill_data(floe_pos);
%
% Other m-files required: none
% Subfunctions: none
% MAT-files required: none
%
% See also: R_DATA_POS

% Author: Marie-Pier Marquis, Dany Dumont
% UQAR/ISMER
% email: dany_dumont@uqar.ca
% Website: http://www.ismer.ca/dumont-dany
% August 2014, Last revision : October 2014
% ________________________________________________

m     = length(data);
count = 0;             % Count number of nan values
for i = 1 : m
    f = data(i);
    
    if isnan(f)
        count = count + 1; 
    end
    
    % Replace nan values by the last non nan value 
    if ~isnan(f) && count ~= 0
        for j = 1 : count
            data(i-j) = f;
        end
        count = 0;       
    end
    
    % If the last values are nan values, remember the last non nan value
    if ~isnan(f)
        no_nan = f;
    end
end

% Replace de last nan values by the last non nan value
if isnan(data(m))
    for j = 0:count
        data(m-j) = no_nan;
    end
end
            
data_f = data;
end