Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

72 views
Skip to first unread message

Rob Newman

unread,
Nov 6, 2007, 7:39:30 PM11/6/07
to
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.

Walter Roberson

unread,
Nov 6, 2007, 8:07:41 PM11/6/07
to
In article <fgr1g2$t5g$1...@fred.mathworks.com>,

Rob Newman <rlne...@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

Dan Haeg

unread,
Nov 7, 2007, 11:50:18 AM11/7/07
to
"Rob Newman" <rlne...@ucsd.edu> wrote in message
<fgr1g2$t5g$1...@fred.mathworks.com>...

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

Walter Roberson

unread,
Nov 7, 2007, 2:27:37 PM11/7/07
to
In article <fgsqca$dbc$1...@fred.mathworks.com>, Dan Haeg <ha...@msoe.edu> wrote:
>"Rob Newman" <rlne...@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

Rob Newman

unread,
Nov 7, 2007, 5:30:37 PM11/7/07
to
Thanks everyone for your help - I am trying to implement this now.

Best regards.

Dirco du Toit

unread,
Mar 11, 2008, 8:02:02 AM3/11/08
to
> 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.)

Ken

unread,
Nov 4, 2009, 10:18:02 AM11/4/09
to
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" <fa...@dummy.com> wrote in message <fr5sbq$rcn$1...@fred.mathworks.com>...

0 new messages