Time difference calculated with datenum

3 views (last 30 days)
Hi,
This is my first time posting in the mathworks forums, so hello everybody!
Anyway, this is my question:
I have a time series of data at intervals of 20 min, and am running a script to check if all the intervals are equal (i.e. 20 min between each data set). To check the intervals, I have something like this:
diff_dates=diff([time series]) % [time series] is in matlab serial date number format
if sum(diff_dates-diff_dates(1))==0 % this tells me if the intervals are equal
The problem is, in checking the data manually the intervals seem equal, but diff_dates-diff_dates(1) gives me a non zero vector like this:
1.0e-09 *
[
0
0
0
0
0
0
0.116415321826935
0
0
0
0
0
0
0
0
0.116415321826935
0
0
0
0
0
0
0
0
0.116415321826935]
and so on...
By observation, the non-zero differences repeat every 9 elements and in tracing back, they correspond to the following times:
15:55:39 minus 15:35:39
18:55:39 minus 18:25:39
21:55:39 minus 21:35:39
and so on...Basically every 3 hours. Also, I tried testing with different hours, minutes and seconds and strangely the difference only appears for those specific times listed above. Changing the seconds value does not have any effect on the calculated difference.
I managed to solve this problem by working in datevec format rather than in serial date format, but its really bugging me what's causing this inconsistency. My idea is that the matlab serial date format is expressed as a fraction of a day and the precision of the datenum function (which I had used to convert my data to serial date) is causing this.
Any experts around here willing to shed some light? Thanks in advance!
-XRT

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 23 Jul 2014
The difference is very small: 1.0e-09, it's probably due to the way your data are stored. Look at this link http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

More Answers (0)

Community Treasure Hunt

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

Start Hunting!