View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

4.7 | 72 ratings Rate this file 247 Downloads (last 30 days) File Size: 10.5 KB File ID: #3486 Version: 1.3
image thumbnail



Brian Katz (view profile)


23 May 2003 (Updated )

Rotate XTickLabel to better accommodate long labels and text labels.

Editor's Notes:

This file was a File Exchange Pick of the Week

| Watch this File

File Information

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


Xticklabel Rotate90 and Hatched Fill Patterns inspired this file.

This file inspired Protein Array Tool, Rotate X Tick Labels For Multi Axes, Yticklabel Rotate, and X Yrotalabel Rotate X Axis And Y Axis Labels.

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (100)
06 Dec 2016 Donghoon Yeo

nice work thanks :)

Comment only
30 Nov 2016 Bob

Bob (view profile)

Great Utility but this fails as of R2016b due to the following line:

if ~exist('xTickLabels')

This should be changed to:

if ~exist('xTickLabels','var')

The reason is because there now exists a file named xticklabels:

>> which xTickLabels
C:\Program Files\MATLAB\R2016b\toolbox\matlab\graph3d\xticklabels.m

The old statement will always return true regardless of whether the local variable exists or not, resulting in the following error:

Undefined function or variable 'xTickLabels'.

Error in xticklabel_rotate (line 124)
if length(XTick) ~= length(xTickLabels),

Replacing the line to add the var qualifier checks only for variables and resolves the issue:

if ~exist('xTickLabels','var')

08 Oct 2016 saeid safaei

01 Oct 2016 Yanbing Jia

25 Aug 2016 jhlasheras

working grate in 2012a. Cheers

03 Aug 2016 chlor thanks

I used this line in my GUI and my axes shrinked and shifted, fyi...

Comment only
17 Jun 2016 praveen sagar

i am using matlab 2013a ,how to fit this code in my m.file i am a new user using file exchange i want my xTick label to rotate +45 .

Comment only
08 Feb 2016 Daniel Thomas

This code is still useful when a user has a version lower than 2014b. Overlap issue can be resolved by first having right size of figure and then using this function. But one problem I am experiencing with the code is that sometimes when it is used in subplot, the overlap issue happens again in only last subplot.

24 Jan 2016 Andrea

Andrea (view profile)

When putting labels on top and rotating them by a negative angle they overlap with the figure. Can someone help me to solve this?

Comment only
13 Oct 2015 Jannis Reich

25 Mar 2015 evinaa SELLAIAH

17 Mar 2015 Stefan Countryman

06 Mar 2015 Michelle Hirsch

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.

ax = gca;
ax.XTickLabelRotation = 45;

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

Comment only
22 Feb 2015 pr

pr (view profile)

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?

Comment only
12 Feb 2015 Holger Hoffmann

works perfect

23 Sep 2014 Brian Katz

Brian Katz (view profile)

@Moroni : Please look at the help for the correct usage, as you are not using the function correctly.

For your example, simply use:

Comment only
17 Sep 2014 Moroni

Moroni (view profile)

I am getting this error everytime
Undefined function 'xticklabel_rotate' for input arguments of type 'double'.

stock_labels = {'Market','3M','ALCOA','ALTRIA','Amex'};

help please!

Comment only
04 Sep 2014 Brian Katz

Brian Katz (view profile)

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.

Comment only
24 May 2014 Simon

Simon (view profile)

16 May 2014 Ivan

Ivan (view profile)

16 May 2014 Ivan

Ivan (view profile)

08 Apr 2014 Erik

Erik (view profile)

04 Mar 2014 Sabri Mutlu

22 Jan 2014 Andreas Kraushaar

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 works fine for us instead.

15 Jan 2014 MoonKnight

12 Sep 2013 Krishna

There might be small glitch,while using this rotation on a bar graph with positive and negative variations and long texts for ticks, a part of the labels ended up being positioned inside the graph. :(

09 May 2013 Scott Teuscher

when using a pareto chart which has two yticks, the yticks on the left stay the same but the yticks on the right get moved so they no longer align with the data.

Comment only
08 Feb 2013 Masatoshi

18 Aug 2012 Faezeh Safari

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.

Comment only
13 Jul 2012 Shahar Shlomi

Thanks so much!
I've downloaded the file and simply added xticklabel_rotate to my code. Magic!

06 Mar 2012 ted p teng

ted p teng (view profile)

21 Feb 2012 Julian

Julian (view profile)

Haven't worked it hard - seemed to do what I needed out of the box - so thanks very much.

13 Oct 2011 Ziv

Ziv (view profile)

just what i needed. few minor issues but got the hang of it in no time.
Ziv yekutieli

08 Sep 2011 Boi-Yee Liao

thank you

02 Sep 2011 gda

gda (view profile)

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

Can anyone help me? thanks

Comment only
27 Aug 2011 Minli

Minli (view profile)

this helps a lot. thank you !!

10 Aug 2011 Corinne

14 May 2011 Pepa

Pepa (view profile)

02 May 2011 Brian Katz

Brian Katz (view profile)

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.

Comment only
02 May 2011 Brian Katz

Brian Katz (view profile)

It seems that the problem seen by Daniel Visser is related to the newer version of boxplot. I am not currently running v.2010 so I cannot examine this issue further at this time. Sorry.

Comment only
15 Feb 2011 m s

m s (view profile)

thanks a lot.

Comment only
09 Feb 2011 Pratik Chhatbar

it works!! will save a lot of my time!!! yay!!!!

08 Feb 2011 Lindsey

??? Error using ==> xticklabel_rotate at 117
xticklabel_rotate : must have same number of elements in "XTick"
and "XTickLabel"

I have checked that I have 29 ticks and 29 labels

Comment only
21 Jan 2011 Denzel Li

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:

Please take a look and any suggestion will be highly appreciated.

15 Jan 2011 Ian

Ian (view profile)

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.

Comment only
17 Dec 2010 Alecsander Eitan

19 Oct 2010 Julian

Julian (view profile)

Thanks very much, handy utility (I've not tried working it hard - seemed to do what I needed out of the box)

Comment only
18 Oct 2010 Daniel Visser

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'})
%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

28 Aug 2010 Nick Mariette

Nice one. Just what I needed. Thanks Brian!

19 Aug 2010 Brian Katz

Brian Katz (view profile)

Please note that, as stated in the function and the error message above, XTickLabel_Rotate CANNOT be called multiple times on the same plot. Calling a new figure, the example works fine.

Comment only
02 Jun 2010 Li

Li (view profile)

The example does not work:
??? Error using ==> xticklabel_rotate at 71
xticklabel_rotate : can not process, either xticklabel_rotate has already been
run or XTickLabel field has been erased

Comment only
28 May 2010 Aaron

Aaron (view profile)

Useful, but labels are offset (as are titles) when using multiple subplots (specifically, a 2x2 array of subplots).

26 Apr 2010 Diego Barragán

Diego Barragán (view profile)

22 Dec 2009 Benjamin

a fast and easy solution is to comment

set(gca,'position',newpos) ;

and, if necessary, set the new position manually

for me, with a few changes, it works very well

21 Dec 2009 Benjamin

I have the same Problem

Error in ==> xticklabel_rotate at ...

Seems to be a problem because I changed the XAxisLocation to 'Top'

I will check if there is a simple solution if there is solution yet.

Comment only
23 Nov 2009 Theresa

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.

17 Nov 2009 bob

bob (view profile)

Problem, same as les ander:

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

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

any help?


Comment only
15 Oct 2009 Tove

Tove (view profile)

30 Sep 2009 Theresa

Another update: using the full command works with rotations of 90 degrees for me.

i.e. xticklabel_rotate([], 90, []) works well, but xticklabel_rotate does not work. Now that I can use the full functionality of this nice program I want to update my original rating.

31 Aug 2009 Theresa

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.

30 Apr 2009 Salifou

Great Function. Thanks a bunch! It saved me from caving in to Stata.

Comment only
11 Mar 2009 Gilles Criton

Thank you very much, so useful !

24 Feb 2009 Etsuko

Etsuko (view profile)

13 Feb 2009 Fredrik

12 Feb 2009 Tue

Tue (view profile)

05 Jan 2009 Federico De Masi

THanks... tens of boxplots to process. Not automated thanks to this script.

25 Jul 2008 flicker 420

Thanks..Nice work

06 Jul 2008 M Shaw

Very helpful and well documented. Thanks!!

03 Jul 2008 Peter Pan

but this rating system is confusing.

The function is perfect, just a small buck.

03 Jul 2008 Peter Pan

Nice function, thanks a lot!

There is a small error, it occurs just when only one XTickLabelName is given.

For example:
bar( [ 1 ] ); xticklabel_rotate;

It claims on line 160, cause get(htext, 'extent' ) gives no cell, but an array back.
My solution:
x = get( hText, 'extent' );
if iscell( x ) == true
textsizes = cell2mat( x ) ;
textsizes = x;
with line 160 deleted works.


20 May 2008 Korey Y

Great function. I had to place my X-axis labels at the top and therefore have them left-aligned so I added a few lines somewhere around line 141:

xAxisLocation = get(gca, 'XAxisLocation');
if (xAxisLocation == 'top')

Comment only
17 Apr 2008 Chris Lowis

Brilliant, solved my problem with a single function call using the default options.

20 Feb 2008 David K

Excellent utility. It almost needs to be part of matlab. This is so essential.

21 Dec 2007 Natalia Rivera

This is really helpful. Thank you!

03 Dec 2007 Patrick Freeman

Very useful function for displaying long labels. Good job!

01 Dec 2007 Brent Bergan

As soon as you zoom in, the tick labels do not follow appropriately. Too bad, this would have been a great function

12 Nov 2007 Greg Heins

Awesome - worked first time - like the TeX ability.

12 Nov 2007 Greg Heins

Awesome - worked first time - like the TeX ability.

31 Aug 2007 George C

just when i was about to switch to excel, i found this! MATLAB rocks!!

08 Aug 2006 Nathan Pust

Very helpful code.
if lines 68-77 are changed to as follows, the code will allow the current text labels to be used and property value pairs to be changed for those labels. Example usage:


Lines 68-76:
if (nargin < 3 || isempty(varargin{1})) & (~exist('XTick') | isempty(XTick)),
xTickLabels = get(gca,'XTickLabel') ; % use current XTickLabel
% remove trailing spaces if exist (typical with auto generated XTickLabel)
temp1 = num2cell(xTickLabels,2) ;
for loop = 1:length(temp1),
temp1{loop} = deblank(temp1{loop}) ;
xTickLabels = temp1 ;
varargin = varargin(2:length(varargin));

10 Feb 2006 Stanley Bileschi

works exactly like I wanted it to

29 Dec 2005 Brian Katz

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

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 1.1

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 1.2

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

23 Sep 2014 1.3

2014 : Allow for multiline labels

Contact us