How to solve floating point problem with datenum
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
dpb
el 6 de Ag. de 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.
Respuestas (1)
Peter Perkins
el 24 de Ag. de 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.
Ver también
Categorías
Más información sobre Time Series Objects en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!