File Exchange

image thumbnail

ticklabelformat - set a dynamic format of axes tick labels

version 1.5 (5.4 KB) by

ticklabelformat sets the axes tick labels format. The format is preserved during zoom/pan/maximize

11 Downloads

Updated

View License

ticklabelformat sets the format of an axes tick labels based on sprintf format specifiers. The format is preserved during zoom/pan/maximize and applied to the dynamic tick values.

Calling ticklabelformat with an empty ([] or '') format will revert to Matlab's normal tick labels display behavior.

Syntax:
ticklabelformat(hAxes, axName, format)

Input Parameters:
hAxes - handle to the modified axes, such as returned by the gca function
axName - name(s) of axles to modify: 'x','y','z' or combination (e.g. 'xy')
format - format of the tick labels in sprintf format (e.g. '%.1f V') or a
function handle that will be called whenever labels need to be updated

Examples:
ticklabelformat(gca,'y','%.6g V') - sets y axis in current axes to display 6 significant digits
ticklabelformat(gca,'xy','%.2f') - sets x & y axes in current axes to display 2 decimal digits
ticklabelformat(gca,'z',@myFcn) - sets a function to update the Z tick labels in current axes
ticklabelformat(gca,'z',{@myFcn,extraData}) - sets an update function as above, with extra data

Warning:
This code heavily relies on undocumented and unsupported Matlab functionality. It works on Matlab 7+, but use at your own risk!

Technical description and more details:
http://UndocumentedMatlab.com/blog/setting-axes-tick-labels-format
http://UndocumentedMatlab.com/blog/undocumented-hg2-graphics-events

Bugs and suggestions:
Please send to Yair Altman (altmany at gmail dot com)

Comments and Ratings (15)

These two lines after creating axes may be helpful. Lot's of options for ytickformat.

hAxes.YAxis.ExponentMode = 'manual';
ytickformat('%,g')

Very useful function indeed. However, I also see a large increase in CPU usage (40%, i7 dual-core) after using it in R2016b, but not in R2013a. Profiler shows a large number of calls to 'adjust_ticklbl' (~2000 in 10s), so maybe something keeps triggering updates under HG2 or in R2016b. Test code:
plot([1 2 3], [1 2 3]);
ticklabelformat(gca, 'xy', '%.1f');

Lukas

Lukas (view profile)

Script works fine, but requires a lot of calculations/updates. The "profile viewer" helped me to figure out that this script was the reason for my GUI beeing extremly slow (the GUI has 14 axes in one figure). I used
ticklabelformat(hAxes,'y','%.3g');

Lukas

Lukas (view profile)

Script works fine, but requires a lot of calculations/updates. The "profile viewer" helped me to figure out that this script was the reason for my GUI beeing extremly slow (the GUI has 14 axes in one figure).

Erica Coenen

Great script!
Could you please post an example of an update function: myFcn? That would be really helpful.
Thanks.

Very nice. A minor issue that I want to report. If you save the figure as a matlab figure and you reload it, it will generate an error.
The figure will open and it will have the formatted labels but they will not auto-update anymore. You will have to manually rerun the ticklabelformat command. Matlab 2015b.

PS
tested with 2015b

Chang hsiung

great, thanks a lot.

Yair Altman

Yair Altman (view profile)

@Chang - set(gca,'FontSize',20)

Chang hsiung

can you change the font size ?
somehow in my Matlab 2014b version, I can not change the font size of ticklabels anymore ?

Robert

Robert (view profile)

This is a great function! I especially like the function handle formatting option.

I noticed that the code for R2014b and newer (HG2) needed a few tweaks to:

1) work when format was a function handle (when the labels are updated for the first time, the code assumes format is a string for sprint) and

2) to delete prior listeners when format is empty (I think the hRuler listener sticks around even when the hAxes one is removed).

Christian

Great script. Callback functionality for formatting is also very useful.

Excellent script. It was very useful. Thank you for share it

Mayi

Mayi (view profile)

Very good, I have use it in my application. Tanks

Updates

1.5

fixes suggested by user Robert for R2014b (HG2)

1.4

fix for R2014b (HG2)

1.3

updated for R2014b (HG2)

1.1

Clarified summary & description

MATLAB Release
MATLAB 7.14 (R2012a)
Acknowledgements

Inspired: offsetTicks

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

» Watch video