MATLAB axis relabeling
June 12, 2007 7:55 AM   RSS feed for this thread Subscribe

Customizing MATLAB axes: Transform unixtime into date labels?

MATLAB gurus:

My figure's X axis currently consists of unix timestamps. I would like it to be labeled as YYYY/MM instead, and I'd like MATLAB to continue to work its magic as far as redrawing the axis prettily when the window is resized.

I found this library to convert unixtimes to year/month and this tutorial on customizing axes, but I still need some more help. (I can convert the times just fine, it's the axis stuff I don't understand.)
posted by dmd to computers & internet (5 comments total)
Where specifically are you getting stuck?

MATLAB axes have things called properties. One of these properties is called XTickLabel, and as you might suspect, it is what stores the labels for the ticks along the X axis. You set properties in MATLAB by doing set(h,propname,val), where h is the handle of the object whose properties you are trying to change, propname is the name of the property, and val is it's new value.

What you want to do is set the XTickLabel property to your new array of converted timestamps. Suppose you have the converted timestamps stored in x. You then do set(h,'XTickLabel',x), and you're done.

Something you need to remember is that because you are using a YYYY/MM format, you have a non-numeric character in each tick label. This means that x needs to be either a character array or a cell array of strings. So your tick label array should look like one of these two:

x = ['2007/01'; '2007/02']
x ={'2007/01' '2007/02'}

The advantage of the latter is that the strings don't all need to be the same length.
posted by epugachev at 10:12 AM on June 12, 2007


You can tick labels and locations using the set command. Set XTick to start:interval:end, to create ticks at the start point, and every interval after, until the end point. Set XTickLabel to a cell array of strings of the label text you want.
posted by Chuckles at 10:13 AM on June 12, 2007


Oh, and you might be wondering how to get the axis handle h. The variable gca (presumbly stands for "get current axis") stores the id number of the last axis you plotted something in. So you can use gca in place of h above.

If this simple solution won't work in your situation, there are other ways to get the handle you're looking for.
posted by epugachev at 10:18 AM on June 12, 2007


Here's an M-file I came up with:

function fixLabels()
xTicks = get(gca,['x' 'Tick']); % get the x values for each tick mark
dates = datenum(unixsecs2date(xTicks)); % convert each timestamp into a date
xTickLabels = datestr(dates,'yyyy/mm'); % convert each date into a string
set(gca,['y' 'TickLabel'], xTickLabels); % label each tick with a string
set(gcf,'ResizeCmd',fixLabels); % do it again whenever the plot is resized


Call fixLabels after plotting your graph. I haven't tested it fully, but it should do what you're asking.
posted by wtdoor at 10:32 AM on June 12, 2007


That link should have been this: Specifying Ticks and Tick Labels.
posted by Chuckles at 10:41 AM on June 12, 2007


« Older How do you just move on with y...   |   What is a good way to share th... Newer »
This thread is closed to new comments.