How to solve floating point problem with datenum

2 次查看(过去 30 天)
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');
next_date_time=datenum(str_next_date,'yyyy-mm-dd HH:MM:SS');
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
% then find missing datnums
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 个评论
dpb 2018-8-6
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.


回答(1 个)

Peter Perkins
Peter Perkins 2018-8-24
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.


Help CenterFile Exchange 中查找有关 Logical 的更多信息




Community Treasure Hunt

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

Start Hunting!

Translated by