How to fill the gap with Nan

2 次查看(过去 30 天)
Khairul Afifi
Khairul Afifi 2015-2-2
回答: Guillaume 2015-2-2
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.

回答(1 个)

Guillaume
Guillaume 2015-2-2
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, :);

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by