Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Datenum converting to inconsistent format

Subject: Datenum converting to inconsistent format

From: Andrea

Date: 7 Jan, 2012 12:56:08

Message: 1 of 6

Hi,

If I do datestr(1/24, 'HH:MM') I get:
ans =

01:00

However, when doing datenum ('01:00', 'HH:MM') I get:

ans =

    7.348690416666666e+005

Now, I understand that the function has a period of 24/24 (that is, 1/24 and 25/24 returns the same value), but using datenum I would expect to get the smallest multiple, that is 1/24 in this case. Is there some setting that needs to setup?

Thanks

Subject: Datenum converting to inconsistent format

From: dpb

Date: 7 Jan, 2012 14:34:56

Message: 2 of 6

On 1/7/2012 6:56 AM, Andrea wrote:
> Hi,
>
> If I do datestr(1/24, 'HH:MM') I get:
> ans =
>
> 01:00
>
> However, when doing datenum ('01:00', 'HH:MM') I get:
>
> ans =
>
> 7.348690416666666e+005
>
> Now, I understand that the function has a period of 24/24 (that is, 1/24
> and 25/24 returns the same value), but using datenum I would expect to
> get the smallest multiple, that is 1/24 in this case. Is there some
> setting that needs to setup?

Working as advertised and "no"...

doc datenum

"A serial date number represents the whole and fractional number of days
from a specific date and time, where datenum('Jan-1-0000 00:00:00')
returns the number 1. (The year 0000 is merely a reference point and is
not intended to be interpreted as a real year in time.)"

If you want the hour fraction from datenum you have to subtract the
reference value.

BTW, 1/24 and 25/24 don't return the same value unless you truncate the
day portion as you did by the specific use of "HH:MM" above try

t=[1 25]/24;
datestr(t,0)

and you'll see there is more symmetry than you recognize in the functions.

--

Subject: Datenum converting to inconsistent format

From: dpb

Date: 7 Jan, 2012 15:27:20

Message: 3 of 6

On 1/7/2012 8:34 AM, dpb wrote:
...

> If you want the hour fraction from datenum you have to subtract the
> reference value.

Or supply the reference date explicitly...

--

Subject: Datenum converting to inconsistent format

From: Andrea

Date: 7 Jan, 2012 16:55:08

Message: 4 of 6

dpb <none@non.net> wrote in message <je9o8l$3kv$1@speranza.aioe.org>...
> On 1/7/2012 8:34 AM, dpb wrote:
> ...
>
> > If you want the hour fraction from datenum you have to subtract the
> > reference value.
>
> Or supply the reference date explicitly...
>
> --

Hi dpb,

Chanks for the clarification, I assumed that when you use datenum with a time, with no date provided, the function would give some absolute reference to the time.

Interesting, looks like the default date added is "1-Jan-2012"

>> datenum('01:00', 'HH:MM')-datenum('01-Jan-2012')

ans =

   0.041666666627862

>> datenum('00-jan-0000 01:00' , 'dd-mmm-yyyy HH:MM')

ans =

   0.041666666627862


Any idea if it refers the beginning of each month or the beginning of the year?
For the type of calculations that I'm doing it is less time consuming to subtract the reference date (rather than to indicate 00-jan-0000)

Subject: Datenum converting to inconsistent format

From: dpb

Date: 7 Jan, 2012 17:41:55

Message: 5 of 6

On 1/7/2012 10:55 AM, Andrea wrote:
...

> Chanks for the clarification, I assumed that when you use datenum with a
> time, with no date provided, the function would give some absolute
> reference to the time.
>
> Interesting, looks like the default date added is "1-Jan-2012"
>
>>> datenum('01:00', 'HH:MM')-datenum('01-Jan-2012')
>
> ans =
>
> 0.041666666627862
>
>>> datenum('00-jan-0000 01:00' , 'dd-mmm-yyyy HH:MM')
>
> ans =
>
> 0.041666666627862
>
>
> Any idea if it refers the beginning of each month or the beginning of
> the year?

...

Check documentation for the version of Matlab you're using--there have
been some modifications/additions to datenum() syntax over various releases.

In my release, datenum() calls a C mex-function the source for which is
provided so you can see what the internal logic actually is. Whether
TMW continued the philosophy w/ later releases I'm uncertain.

The current doc is ambiguous for the syntax you used I think (my
reading, maybe a more careful parsing would reveal the answer). My first
scanning indicates the default pivot year is currentyear-50 but that
doesn't jibe w/ your empirical test altho there's still the question of
what your version is.

Actually in my release, the original return value is what you were
expecting; knowing there had been changes I consulted the online current
version doc's before answering.

--

Subject: Datenum converting to inconsistent format

From: Steven_Lord

Date: 9 Jan, 2012 02:56:39

Message: 6 of 6



"Andrea " <larosa.andre@gmail.com> wrote in message
news:je9tdc$je3$1@newscl01ah.mathworks.com...
> dpb <none@non.net> wrote in message <je9o8l$3kv$1@speranza.aioe.org>...
>> On 1/7/2012 8:34 AM, dpb wrote:

*snip*

> Chanks for the clarification, I assumed that when you use datenum with a
> time, with no date provided, the function would give some absolute
> reference to the time.
>
> Interesting, looks like the default date added is "1-Jan-2012"
>
>>> datenum('01:00', 'HH:MM')-datenum('01-Jan-2012')
>
> ans =
>
> 0.041666666627862
>
>>> datenum('00-jan-0000 01:00' , 'dd-mmm-yyyy HH:MM')
>
> ans =
>
> 0.041666666627862
>
>
> Any idea if it refers the beginning of each month or the beginning of the
> year?
> For the type of calculations that I'm doing it is less time consuming to
> subtract the reference date (rather than to indicate 00-jan-0000)

http://www.mathworks.com/help/techdoc/matlab_prog/bspgcx2-1.html#bspgder-1

"Certain formats may not contain enough information to compute a date
number. In these cases, hours, minutes, seconds, and milliseconds default to
0, the month defaults to January, the day to 1, and the year to the current
year."

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us