How to fill the gap with Nan

3 views (last 30 days)
Khairul Afifi
Khairul Afifi on 2 Feb 2015
Answered: Guillaume on 2 Feb 2015
Greetings,
I would like to ask on how to fill the gap with Nan. The file below show from 1 Feb 2013 to 13 Feb 2013, the data are missing. I would like to fill the missing data with Nan. Thank you in advance.

Answers (1)

Guillaume
Guillaume on 2 Feb 2015
Build your cell array with the date/time including the missing values and NaN/empty strings everywhere else, and use intersect to fill it with the values you already have:
olddates = datenum(GAN_2013(:, 1), 'mm/dd/yyyy HH:MM'); %convert dates to numbers so we can get min and max
startdate = min(olddates);
enddate = max(olddates);
alldates = datestr(startdate:1/(60*24):enddate, 'mm/dd/yyyy HH:MM');
new_GAN = [num2cell(alldates, 2), cell(size(alldates, 1), 1), num2cell(nan(size(alldates, 1), 6))]; %destination cell array
%unfortunately, your dates are in format not supported by matlab, so convert them to the same format as new_GAN:
olddates = num2cell(datestr(olddates, ''mm/dd/yyyy HH:MM'), 2);
[~, inew, iold] = intersect(alldates, olddates);
new_GAN(inew, :) = GAN_2013(iold, :);

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!