How to solve floating point problem with datenum
1 view (last 30 days)
Show older comments
I want to find missing dates from a cell-array of dates. I have attached the cell-array with the question. My solution to the problem was as follows:
% create an array of time-steps with every datenum from starting to end date at required time-steps;
begin_date_time=datenum('2011-01-06 00:00:00','yyyy-mm-dd HH:MM:SS');
str_next_date=strcat('2011-01-06',32,'00:15:00');
next_date_time=datenum(str_next_date,'yyyy-mm-dd HH:MM:SS');
interval=next_date_time-begin_date_time;
end_date_time=datenum('2011-12-31 00:00:00','yyyy-mm-dd HH:MM:SS');
full_data_time_steps=begin_date_time:interval:end_date_time; % array of datenums containing all the time-steps between start and end dates with 15min interval
% convert the given cell arary to datenums
avail_time_steps=cellfun(@datenum,time_steps);
% then find missing datnums
[~,ind_full_data_tstep]=intersect(full_data_time_steps,...
avail_time_steps);
ind_missing=setdiff(1:length(full_data_time_steps),ind_full_data_tstep); % array of missing datenums
But it gives unexpected results because of floating-point errors. for example, it says that the date '2011-01-06 00:30:00' is missing from the cell-array 'time-steps', but it actually exists.
I don't know how to fix this issue. Any help is appreciated.
2 Comments
dpb
on 6 Aug 2018
I answered the previous Q? to show how to generate the datenum vector to match.
One can "cheat" and use ismembertol but that's more cumbersome and faking the right answer.
Answers (1)
Peter Perkins
on 24 Aug 2018
The real answer is, use datetime if at all possible, because this kind of floating point issue doesn't exist in datetime (at least not until you get to much higher resolutions).
But I gather that you are using R2014a, so that's not an option.
See Also
Categories
Find more on Time Series Objects 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!