File Exchange

image thumbnail


version (4.32 KB) by Brian Katz
Rotate XTickLabel to better accommodate long labels and text labels.


Updated 19 Apr 2021

View Version History

View License

Editor's Note: This file was a File Exchange Pick of the Week

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

Cite As

Brian Katz (2021). XTICKLABEL_ROTATE (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (108)

Brian Katz

Dear Zhong Ziqian, I understand you may have problems but your comment is insufficient in information and incorrect. I am currently using version ' (R2020b) Update 1' and this function works fine. Please provide an example script of what you tried and what error you received.
-Brian Katz

Zhong Ziqian

It does not work because 'xTickLabels' can not be found. R2020b

George Pinto

Brian Katz

Linhong, as stated, it seems like you tried to run xticklabel_rotate twice on the same figure/axes, which is not possible. clear figure/axis, plot and run again.

Brian Katz

See Michelle's comment (6-Mar-2015): This function should be obsolete; as of R2014b, MATLAB now has the built-in ability to rotate tick labels.
Of course for those working with older versions at times, it still comes in handy... :)

Lihong Ren

Error using xticklabel_rotate (line 86)
xticklabel_rotate : can not process, either xticklabel_rotate has already been run or
XTickLabel field has been erased

Christopher Beckett

Bob's fix (30 Nov 2016) works well in R2017a

Pascal R

It seems to be shifting my Xticklabel, creating an offset between the stemplot (or barplot) and the Xticklabels. So eventually, the labels and bars are staggered.

Otherwise, it works well.

Donghoon Yeo

nice work thanks :)


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

saeid safaei

Yanbing Jia


working grate in 2012a. Cheers

chlor thanks

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

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 .

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.


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

Jannis Reich


Stefan Countryman

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!


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?

Holger Hoffmann

works perfect

Brian Katz

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

For your example, simply use:


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!

Brian Katz

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.





Sabri Mutlu

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.



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. :(

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.


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.

Shahar Shlomi

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



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


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

Boi-Yee Liao

thank you


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


this helps a lot. thank you !!



Brian Katz

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.

Brian Katz

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.

m s

thanks a lot.

Pratik Chhatbar

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


??? 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

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.


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.

Alecsander Eitan


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

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

Nick Mariette

Nice one. Just what I needed. Thanks Brian!

Brian Katz

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.


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


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

Diego Barragán


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


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.


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.


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?




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.


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.


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

Gilles Criton

Thank you very much, so useful !




Federico De Masi

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

flicker 420

Thanks..Nice work

M Shaw

Very helpful and well documented. Thanks!!

Peter Pan

but this rating system is confusing.

The function is perfect, just a small buck.

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.


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

Chris Lowis

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

David K

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

Natalia Rivera

This is really helpful. Thank you!

Patrick Freeman

Very useful function for displaying long labels. Good job!

Brent Bergan

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

Greg Heins

Awesome - worked first time - like the TeX ability.

Greg Heins

Awesome - worked first time - like the TeX ability.

George C

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

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));

Stanley Bileschi

works exactly like I wanted it to

Brian Katz

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

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.

Fangwen Lu

It's cool!

Kay Pham

This is really useful! GREAT!

Kay Pham

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

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

Isabella Osetinsky

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

mark drever

this was sweet.

les ander

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

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

Prasanth Karumanchi

awesome stuff

Yuval Cohen

Other than the zoom issue, perfect. BRAVO!

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.


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

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

Imola Fodor

Awesome. Thanks!

Alf Magnusson

You made my day (nigth) Thanks

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.

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.

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

MATLAB Release Compatibility
Created with R2007a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!