MATLAB axis relabeling
June 12, 2007 7:55 AM   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 (4 answers total) 1 user marked this as a favorite
 
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


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


« Older Internal IP addresses fail to resolve   |   How can I share 50+ podcasts? Newer »
This thread is closed to new comments.