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');
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
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
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 Center 및 File Exchange에서 Time Series Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!