Editor's Notes:

A modified version of XTICKLABEL_ROTATE90, this version allows for arbitrary angle rotation and most importantly the use of text labels. In addition, an attempt is made to adjust the size and placement of the axis and xlabel to accommodate the new labels.
Example : %Use text labels rotated 45° without tex interpreter
boxplot(randn(5,5),1)
xticklabel_rotate([1:5],45,{'label_1','label_2','label_3','label_4','label_5'},'interpreter','none')

Great news to fans of XTICKLABEL_ROTATE - as of R2014b, MATLAB now has the built-in ability to rotate tick labels (X, Y, and Z). You can control tick label rotation through the axes properties XTickLabel, YTickLabel, and ZTickLabel.

Ex:
plot(1:3)
ax = gca;
ax.XTickLabelRotation = 45;

Huge thanks to Brian for serving the MATLAB community with this great submission since 2003!

I am using 'plotyy' to interpret the data and date/time on x-axis. With the help of this function the labels are rotated successfully. It looks great when the figure is exported. But the 'linkaxes' property for any kind of plot doesn't work. How to solve this issue?

Experience has shown that this function doesn't handle a lot of resizing, which in Matlab can often change axis limits and ticks.
It is best to run it just before exporting, and it is highly recommend to export/print without resizing.

The script does not work in the sense that often the labels are not alligned, at least when saving a graph or when scaling a graph. rotateXLabels.m in http://www.mathworks.com/matlabcentral/fileexchange/27812-rotate-x-axis-tick-labels/all_files works fine for us instead.

It works great with the xticklabel rotation! But, when I save my matlab figure as pdf the pdf file contains rotated version of my matlab figure. I have double check everything and found this function is the cause. I will appreciate if anybody can help me with this.

When I'm calling for xticklabel_rotate function I'm getting the error
??? Error using ==> xticklabel_rotate at 78
xticklabel_rotate : can not process, either xticklabel_rotate has already been run or
XTickLabel field has been erased

I have examine the issue mentioned by Denzel Li. I have tried to reproduce the errors mentioned with only small sucess. I have found an issue with the axis position being changed when y-labels are on the right, which I will look into. I do not get such notable misalignements as you have shown when I run the same examples, so I am not sur how to respond. Please verify that these issues exist when you PRINT the figure, and it is not a simple figure screen display issue.

The text boxes are created at the tick positions, in data units. They are middle aligned, there is not much more that can be done, but I am open to suggestions.

XTICKLABEL_ROTATE is a wonderful tool. Thanks a lot for making it available. How ever, I had the same problem as mentioned by Theresa above, which results in figures hard to use for publish. I also described this issue in great detail on stackoverflow:

I got the following error when running just xticklable_rotate():
Warning: Error occurred while evaluating listener callback.
> In axis>LocSetLimits at 208
In axis at 94
In plotPhases3 at 46
??? Error using ==> set
Values must be finite and non-NaN

The function works fine when there is a single plot, maybe this is because I am using subplots?
I would really like if this function worked.

Hi! Cool stuff!
Unfortunately I Have the same problem as ander and bob with:
figure; boxplot(rand(2));
set(gca,'xtick',[1 2],'xticklabel',{'Tick1' 'Tick2'})
xticklabel_rotate()
%matlab version: 7.10
The error starts at line 160: set(gca,'units','pixel')!?! (Why does the change of unit change the axis size?)
Nevertheless: Thank you so far

The example does not work:
boxplot(randn(5,5),1)
xticklabel_rotate([1:5],45,{'label_1','label_2','label_3','label_4','label_5'},'interpreter','none')
??? Error using ==> xticklabel_rotate at 71
xticklabel_rotate : can not process, either xticklabel_rotate has already been
run or XTickLabel field has been erased

Another update: when you rotate the labels they're not always lined up with each other. Super annoying and makes the figure unusable for publication....which makes the function pretty much useless to me. Sorry...seems like a good start but I've encountered too many small annoying bugs that make the figures publication unfriendly.

Unfortunately, I have not had the success of others with this file. When it rotates the labels, it overlays them onto the plot so they are half over the x-axis and data, and half where they should be. It looks horrible and I can't use the figures that way.

Update: The bug is in rotating 90 degrees. Other rotations work OK. But it's annoying to not have full rotation functionality.

I have re-tested this with changing fonts in the original call and also afterwards via the handle, with no problems...

Comment only

12 Nov 2005

Shai Shen-Orr

If you try setting to a different font through XTickLabel_rotate function call or after the labels has been set - the entire axis is messed up.

29 Oct 2005

Fangwen Lu

It's cool!

10 Aug 2005

Kay Pham

This is really useful! GREAT!

10 Aug 2005

Kay Pham

This is great! It's really useful! Thanks.

08 Jun 2005

Malik Yousef

When i use your code on 4 subplots i get weird results whne one of them that has different axis.
And i see that the subplot become so small!!
How to overcome this problem

Comment only

05 Jun 2005

Isabella Osetinsky

Thank you. Solves many problems!
Add "set(gca,'tickdir','out')" to point out the label location.

26 May 2005

mark drever

this was sweet.

05 May 2005

les ander

??? Error using ==> set
Width and height must be > 0.

Error in ==> xticklabel_rotate at 168
set(gca,'position',newpos) ;

Comment only

01 Oct 2004

Prasanth Karumanchi

awesome stuff

26 Jun 2004

Yuval Cohen

Other than the zoom issue, perfect. BRAVO!

20 Jun 2004

Brian Katz

As this operation removes the current labels and places text objects a tthe appripriate place in the figure window, there is no dynamic link to the axis afterwards, making zoom response difficult. Until MatLab integrates this type of behavoir directly into the label properties I think it would be very difficult to include dynamic behavior.

Comment only

05 Jun 2004

K H

For this and similar functions where text replaces ticks, is there a way to make it react to zooming?

28 May 2004

Anshuman Bansal

Well Done!
I was struggling with this issue since last couple of months! Trying to write my own unsuccessful code..! Saved me a lot of time! I guess it should be part of standard MATLAB package

26 May 2004

Imola Fodor

Awesome. Thanks!

29 Feb 2004

Alf Magnusson

You made my day (nigth) Thanks

03 Feb 2004

John Godward

I spent some time looking for standard Matlab functions to do this with no luck. Then I found this m-file which did what was required. Thanks.

10 Oct 2003

David Sterling

Nicely done! Now I don't have to rotate my plotting axis(eg. barh and horizontal boxplot) to handle the long strings that inevitably creep into my categorical tick labels.

11 Sep 2003

Martin Schaefer

You saved me from using Excel for a whole bunch of Bargraph plots.
Thanks very much for that.
Another nice option would be to edit the ticks freely, that means to allow 2 line ticks and things alike

Updates

11 Jun 2003

Update 11-jun-03 : Resizing of axis is improved with control of xlabel,ylabel, and title. Improved default codition works with PREDEFINED XTickLabels now.

03 Mar 2009

Use suggested modifications have been integrated. The current version now support TOP xlabels. In addition, the degree of resizing of the axis has been reduced.

11 Jun 2010

Rev.2010. Revisions made to improve functionality in response to various user recommendations.