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:
working with time

Subject: working with time

From: Jane T

Date: 24 Jan, 2011 17:32:04

Message: 1 of 11

Hi,

I have a time string of the form 'yyyy/mm/dd HH:MM:SS' which I have converted to a date number using the datenum function - this has allowed me to save the values in an array with other numeric values (see http://www.mathworks.com/matlabcentral/newsreader/view_thread/300663#811546 for more info).

I need to be able to subtract a number of seconds from this time, where the number of seconds may be greater than 60.

I've tried playing with datenum, datevec and datestr functions but I'm struggling to find a consistent solution.

Any help?

Many thanks
Jane

Subject: working with time

From: Jan Simon

Date: 24 Jan, 2011 17:48:04

Message: 2 of 11

Dear Jane,

> I have a time string of the form 'yyyy/mm/dd HH:MM:SS' which I have converted to a date number using the datenum function - ...
> I need to be able to subtract a number of seconds from this time, where the number of seconds may be greater than 60.

If you have DATENUM value adding or subtracting number is trivial:
  v = [2011, 1,24,18, 44, 59]
  n = datenum(datev);
  n_plus_1 = n + 1 / 86400;
  v_plus_1 = datevec(n_plus_1)
The fractional part of the DATENUM is the number of seconds divided by 86400.
See also:
  type etime

Kind regards, Jan

Subject: working with time

From: Jane T

Date: 24 Jan, 2011 17:49:03

Message: 3 of 11

"Jane T" wrote in message <ihkd2k$6t4$1@fred.mathworks.com>...
> Hi,
>
> I have a time string of the form 'yyyy/mm/dd HH:MM:SS' which I have converted to a date number using the datenum function - this has allowed me to save the values in an array with other numeric values (see http://www.mathworks.com/matlabcentral/newsreader/view_thread/300663#811546 for more info).
>
> I need to be able to subtract a number of seconds from this time, where the number of seconds may be greater than 60.
>
> I've tried playing with datenum, datevec and datestr functions but I'm struggling to find a consistent solution.
>
> Any help?
>
> Many thanks
> Jane


If I set interval to an interger number of seconds then

new_time = datenum(datevec(G(ind_end(1),6)) - [0000 00 00 00 00 interval])

where G(ind_end(1),6) is the datenum of the time I wish to subtract from.

Is this the best way of achieving this? I was getting funny results before as I had a bracket in the wrong place! It does seem fairly robust now, even if slightly convoluted.

Is there a simpler way?

Subject: working with time

From: Jane T

Date: 24 Jan, 2011 17:58:04

Message: 4 of 11

"Jan Simon" wrote in message <ihke0j$7dt$1@fred.mathworks.com>...
> Dear Jane,
>
> > I have a time string of the form 'yyyy/mm/dd HH:MM:SS' which I have converted to a date number using the datenum function - ...
> > I need to be able to subtract a number of seconds from this time, where the number of seconds may be greater than 60.
>
> If you have DATENUM value adding or subtracting number is trivial:
> v = [2011, 1,24,18, 44, 59]
> n = datenum(datev);
> n_plus_1 = n + 1 / 86400;
> v_plus_1 = datevec(n_plus_1)
> The fractional part of the DATENUM is the number of seconds divided by 86400.
> See also:
> type etime
>
> Kind regards, Jan

Sorry Jan, your message came in after I had posted my reply.

Thank you,

G(ind_end(1),6) - interval/86400

is certainly easier than my approach. It's just a shame that the datenum documentation didn't make it clear how easy it was. It could have saved me a lot of time!

Thanks again
Jane

Subject: working with time

From: Jan Simon

Date: 24 Jan, 2011 20:46:03

Message: 5 of 11

Dear Jane,

> It's just a shame that the datenum documentation didn't make it clear how easy it was. It could have saved me a lot of time!

Therefore it would be a good idea to send an enhancement report to TMW. This would helpTMW to improve their product --- or to be exact: our product!

Kind regards, Jan

Subject: working with time

From: TideMan

Date: 25 Jan, 2011 00:15:22

Message: 6 of 11

On Jan 25, 6:58 am, "Jane T" <jte...@boreal-laser.com> wrote:
> "Jan Simon" wrote in message <ihke0j$7d...@fred.mathworks.com>...
> > Dear Jane,
>
> > > I have a time string of the form 'yyyy/mm/dd HH:MM:SS' which I have converted to a date number using the datenum function - ...
> > > I need to be able to subtract a number of seconds from this time, where the number of seconds may be greater than 60.
>
> > If you have DATENUM value adding or subtracting number is trivial:
> >   v = [2011, 1,24,18, 44, 59]
> >   n = datenum(datev);
> >   n_plus_1 = n + 1 / 86400;
> >   v_plus_1 = datevec(n_plus_1)
> > The fractional part of the DATENUM is the number of seconds divided by 86400.
> > See also:
> >   type etime
>
> > Kind regards, Jan
>
> Sorry Jan, your message came in after I had posted my reply.
>
> Thank you,
>
> G(ind_end(1),6) - interval/86400
>
> is certainly easier than my approach.  It's just a shame that the datenum documentation didn't make it clear how easy it was. It could have saved me a lot of time!
>
> Thanks again
> Jane

Documentation seldom contains statements of the bleeding obvious.

Subject: working with time

From: Steven_Lord

Date: 25 Jan, 2011 15:15:04

Message: 7 of 11



"Jane T" <jterry@boreal-laser.com> wrote in message
news:ihkejc$faj$1@fred.mathworks.com...
> "Jan Simon" wrote in message <ihke0j$7dt$1@fred.mathworks.com>...
>> Dear Jane,
>>
>> > I have a time string of the form 'yyyy/mm/dd HH:MM:SS' which I have
>> > converted to a date number using the datenum function - ...
>> > I need to be able to subtract a number of seconds from this time, where
>> > the number of seconds may be greater than 60.
>>
>> If you have DATENUM value adding or subtracting number is trivial:
>> v = [2011, 1,24,18, 44, 59]
>> n = datenum(datev);
>> n_plus_1 = n + 1 / 86400;
>> v_plus_1 = datevec(n_plus_1)
>> The fractional part of the DATENUM is the number of seconds divided by
>> 86400.
>> See also:
>> type etime
>>
>> Kind regards, Jan
>
> Sorry Jan, your message came in after I had posted my reply.
>
> Thank you,
>
> G(ind_end(1),6) - interval/86400

I hope that you put a comment before this line of code to explain to other
people reading this code (or yourself, six months from now) what that code
does.

Personally, I'd probably use the somewhat self-documenting ADDTODATE:

http://www.mathworks.com/help/techdoc/ref/addtodate.html

v = [2011, 1,24,18, 44, 59]
n = datenum(v);
newDate = addtodate(n, 70, 'second');
fprintf('70 seconds after %s is %s.\n', datestr(n), datestr(newDate))

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

Subject: working with time

From: Jan Simon

Date: 28 Jan, 2011 14:48:04

Message: 8 of 11

Dear Steven,

> Personally, I'd probably use the somewhat self-documenting ADDTODATE:
> http://www.mathworks.com/help/techdoc/ref/addtodate.html

And the same question as every year:
When has ADDTODATE been added to Matlab?

@TideMan:
It is stated in DOC DATENUM, that it is the "fractional number of days". I'm not sure if the (Number of seconds)/86400 relation is "bleeding obvious" or "undocumented".

Thanks, Jan

Subject: working with time

From: TideMan

Date: 28 Jan, 2011 19:55:32

Message: 9 of 11

On Jan 29, 3:48 am, "Jan Simon" <matlab.THIS_Y...@nMINUSsimon.de>
wrote:
> Dear Steven,
>
> > Personally, I'd probably use the somewhat self-documenting ADDTODATE:
> >http://www.mathworks.com/help/techdoc/ref/addtodate.html
>
> And the same question as every year:
> When has ADDTODATE been added to Matlab?
>
> @TideMan:
> It is stated in DOC DATENUM, that it is the "fractional number of days". I'm not sure if the (Number of seconds)/86400 relation is "bleeding obvious" or "undocumented".
>
> Thanks, Jan

Jan
The OP apparently wants this added to the documentation:
"To convert from seconds to parts of a day, divide by 86400."

My sympathy is with the documentation writers whom she would also have
add:
"To convert from % to proportion, divide by 100."
"To convert from cents to parts of a dollar, divide by 100."
etc
etc

All statements of the bleeding obvious.

Subject: working with time

From: Steven_Lord

Date: 31 Jan, 2011 02:34:03

Message: 10 of 11



"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message
news:ihukv3$jou$1@fred.mathworks.com...
> Dear Steven,
>
>> Personally, I'd probably use the somewhat self-documenting ADDTODATE:
>> http://www.mathworks.com/help/techdoc/ref/addtodate.html
>
> And the same question as every year:
> When has ADDTODATE been added to Matlab?

Release R14, approximately 4 and a half years ago.

http://www.mathworks.com/help/techdoc/rn/f8-1009921.html#f8-1011593

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

Subject: working with time

From: Jan Simon

Date: 31 Jan, 2011 10:12:04

Message: 11 of 11

Dear Steven,

> > When has ADDTODATE been added to Matlab?
> Release R14, approximately 4 and a half years ago.

Thanks! I'm still missing a table of new, remove and modified functions.

@TideMan:
> "To convert from % to proportion, divide by 100."
Unter taking into account that "%" is a comment character, if it is not inside a string or inside another comment.

> "To convert from cents to parts of a dollar, divide by 100."
Of course you mean American cents, and not Euro cents - worth to mention in a scientifically exact documentation.

On the other hand: "percent" and "cent" contain the Latin word for 100 already, so they are more or less self-descriptive. But "whole and fractional number of days" from the DOC (nothing about this in the HELP) leaves some space for interpretations. The fractional part of the date number could be for example the number of milliseconds divided by 86.400.000 also ;-)

BTW: "help sin" reveals that "SIN(X) is the sine of the elements of X.".

Kind regards, Jan

Tags for this Thread

No tags are associated with 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