working with times - unclear receiving values

1 view (last 30 days)
michael
michael on 29 Sep 2022
Edited: dpb on 29 Sep 2022
Hi,
I'm using Matlab R14SP3.
I'm using datenum and datestr functions to perform my data analysis based on time.
startTime=datenum('23:00:00','HH:MM:SS')-datenum('2022','yyyy')+doy2date(260,2022)
endTime=datenum('0:10:00','HH:MM:SS')-datenum('2022','yyyy')+doy2date(261,2022)
Where doy2date is from matlab fileexchange.
Time=datenum(time,'HH:MM:SS')-datenum('2022','yyyy')+doy2date(YearDay,2022*ones(length(time))
stop_time = find(Time>endTime);
if isempty(stop_time)
stop=length(time);
else
stop=stop_time(1);
When checking with datestr for endTime and Time - I see correct data.
When I check the value of endTime and Time I see that Time value is smaller than endTime
For sure, time (and Time) vectors are ending at 1AM.
Any clues how to solve the issue?
  2 Comments
dpb
dpb on 29 Sep 2022
Here's an example of what can go wrong with datenum being just a double...
dn1=datenum(0,0,0,23,0,0)
dn1 = 0.9583
dn1==23/24 % it's just fractional part of a day
ans = logical
1
dn2=(datenum('23:00:00','HH:MM:SS')-fix(datenum('23:00:00','HH:MM:SS')));
dn2==dn1 % but it fails here???
ans = logical
0
dn2-dn1
ans = 3.8805e-11
NB: the size of the difference -- 1E5 X the size of roundoff of a double. That's because the magnitude of the values from which dn2 was computed are full-fledged datenums of the current year and so there's those 5 decimal digits of precision lost in comparison to the direct floating point calculation or use of datenum with an explicit 0 year.
We'll still have to see the actual code and values to see where it went wrong, but that's illustrative that "there be dragons!" in datenum from precision with only the double.

Sign in to comment.

Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Products


Release

R14SP2

Community Treasure Hunt

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

Start Hunting!

Translated by