Thread Subject: Convert x-axis plot labels from epoch seconds to YYYY-MM-DD HH:mm:ss string?

Subject: Convert x-axis plot labels from epoch seconds to YYYY-MM-DD HH:mm:ss string?

From: Rob Newman

Date: 7 Nov, 2007 00:39:30

Message: 1 of 7

Hi there,

I am plotting some data where the x co-ordinate is in epoch seconds (or unix
time, seconds since 1970-01-01 00:00:00). I would like to convert *just* the
x-axis labels to an equivalent time string. For example, if I have the x-axis
labels (automatically created by Matlab):

1089865982
1089867030
1089868078
1089869126
1089870174
1089871222
1089872270


I would like these to display as:

2004-07-15 04:33:02
2004-07-15 04:50:30
2004-07-15 05:07:58
2004-07-15 05:25:26
2004-07-15 05:42:54
2004-07-15 06:00:22
2004-07-15 06:17:50

I have read about the graphics object properties like XTickLabel and XTick,
but I cannot see documented anywhere how to format the tick labels.

Thanks in advance.

Subject: Convert x-axis plot labels from epoch seconds to YYYY-MM-DD HH:mm:ss string?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 7 Nov, 2007 01:07:41

Message: 2 of 7

In article <fgr1g2$t5g$1@fred.mathworks.com>,
Rob Newman <rlnewman@ucsd.edu> wrote:
>I am plotting some data where the x co-ordinate is in epoch seconds (or unix
>time, seconds since 1970-01-01 00:00:00). I would like to convert *just* the
>x-axis labels to an equivalent time string.

>I would like these to display as:

>2004-07-15 04:33:02

After creating the plot, pull out the XTick property of the
axis. Convert those to serial dates and format those serial
dates using format 31 of datestr. Set the axis XTickLabelMode
to 'manual' and set the axis XTickLabel to the formatted dates.

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.

so convert Jan 1 1970 00:00:00 to a serial date once, and
add to that the epoch seconds divided by 24*60*60 to get the
serial date number to format.
--
   "I was very young in those days, but I was also rather dim."
   -- Christopher Priest

Subject: Convert x-axis plot labels from epoch seconds to YYYY-MM-DD HH:mm:ss string?

From: Dan Haeg

Date: 7 Nov, 2007 16:50:18

Message: 3 of 7

"Rob Newman" <rlnewman@ucsd.edu> wrote in message
<fgr1g2$t5g$1@fred.mathworks.com>...
> Hi there,
>
> I am plotting some data where the x co-ordinate is in
epoch seconds (or unix
> time, seconds since 1970-01-01 00:00:00). I would like to
convert *just* the
> x-axis labels to an equivalent time string. For example,
if I have the x-axis
> labels (automatically created by Matlab):
>
> 1089865982
> 1089867030
> 1089868078
> 1089869126
> 1089870174
> 1089871222
> 1089872270
>
>
> I would like these to display as:
>
> 2004-07-15 04:33:02
> 2004-07-15 04:50:30
> 2004-07-15 05:07:58
> 2004-07-15 05:25:26
> 2004-07-15 05:42:54
> 2004-07-15 06:00:22
> 2004-07-15 06:17:50
>
> I have read about the graphics object properties like
XTickLabel and XTick,
> but I cannot see documented anywhere how to format the
tick labels.
>
> Thanks in advance.

See the help for datetick. it may be what you are looking for.
Dan

Subject: Convert x-axis plot labels from epoch seconds to YYYY-MM-DD HH:mm:ss string?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 7 Nov, 2007 19:27:37

Message: 4 of 7

In article <fgsqca$dbc$1@fred.mathworks.com>, Dan Haeg <haegd@msoe.edu> wrote:
>"Rob Newman" <rlnewman@ucsd.edu> wrote in message
><fgr1g2$t5g$1@fred.mathworks.com>...
>> I am plotting some data where the x co-ordinate is in
>epoch seconds (or unix
>> time, seconds since 1970-01-01 00:00:00). I would like to
>convert *just* the
>> x-axis labels to an equivalent time string.

>See the help for datetick. it may be what you are looking for.

datetick requires that the data for the axis must be in
serial date format, which "seconds since 1970-01-01 00:00:00"
is not.

However, since the labels are going to be replaced anyhow,
Rob could convert the x to serial date format and use that as
the x coordinate, and use datetick('x',31). It might make a difference
as to exactly which x are chosen for the tick placement
(matlab likes to place ticks at "nice numbers"), but the result
might be acceptable. It would be slightly easier to implement
than my earlier suggestion.

--
   "No one has the right to destroy another person's belief by
   demanding empirical evidence." -- Ann Landers

Subject: Convert x-axis plot labels from epoch seconds to YYYY-MM-DD HH:mm:ss string?

From: Rob Newman

Date: 7 Nov, 2007 22:30:37

Message: 5 of 7

Thanks everyone for your help - I am trying to implement this now.

Best regards.

Subject: Convert x-axis plot labels from epoch seconds to YYYY-MM-DD HH:mm:ss string?

From: Dirco du Toit

Date: 11 Mar, 2008 12:02:02

Message: 6 of 7

> datetick requires that the data for the axis must
> be in serial date format, which "seconds since
> 1970-01-01 00:00:00" is not.
 
The way I did it was to add the datenum for 1970-01-01 to
my time value, then convert that to a datestr (otherwise it
still looks like a number and not a date).

MyDateNum = (datenum([1970 1 1 0 0 0])*86400 +
MyTimeValue) / 86400;
MyDateStr = datestr(MyDateNum, 'yyyy/mm/dd HH:MM:SS');

Just remember that your custom X-tick labels are now text
values, so it won't scale along with your plot if you
resize it. (I had to make my own GUI to make it recalculate
the dates after every window resize.)

Subject: Convert x-axis plot labels from epoch seconds to YYYY-MM-DD HH:mm:ss string?

From: Ken

Date: 4 Nov, 2009 15:18:02

Message: 7 of 7

Any ideas on how to make that gui? Would it require making controls to do the zooming or can you do it using the standard charting toolbar?

THanks.

"Dirco du Toit" <fake@dummy.com> wrote in message <fr5sbq$rcn$1@fred.mathworks.com>...
> > datetick requires that the data for the axis must
> > be in serial date format, which "seconds since
> > 1970-01-01 00:00:00" is not.
>
> The way I did it was to add the datenum for 1970-01-01 to
> my time value, then convert that to a datestr (otherwise it
> still looks like a number and not a date).
>
> MyDateNum = (datenum([1970 1 1 0 0 0])*86400 +
> MyTimeValue) / 86400;
> MyDateStr = datestr(MyDateNum, 'yyyy/mm/dd HH:MM:SS');
>
> Just remember that your custom X-tick labels are now text
> values, so it won't scale along with your plot if you
> resize it. (I had to make my own GUI to make it recalculate
> the dates after every window resize.)

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
datestr Dirco du Toit 11 Mar, 2008 08:05:07
datenum Dirco du Toit 11 Mar, 2008 08:05:07
epoch time xtic... Rob Newman 6 Nov, 2007 19:40:21
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com