MATLAB: How to create a multi-line tick label for a figure using MATLAB 7.10 (R2010a)

MATLABxtickytickztickzticklabel

I have created a plot and I wish to set XTickLabel and YTickLabel such that it contains multiple lines.

Best Answer

There is no function that will allow you to create a tick label consisting of multiple lines.
As a workaround, you can replace the tick labels with text objects which can contain multiple lines of text. For example:
%%Create figure and remove ticklabels
plot(1:10);
set(gca,'yticklabel',[], 'xticklabel', []) %Remove tick labels
%%Get tick positions
yTicks = get(gca,'ytick');
xTicks = get(gca, 'xtick');
%%Reset the YTicklabels onto multiple lines, 2nd line being twice of first
minX = min(xTicks);
% You will have to adjust the offset based on the size of figure

VerticalOffset = 0.1;
HorizontalOffset = 0.6;
for yy = 1:length(yTicks)
% Create a text box at every Tick label position

% String is specified as LaTeX string and other appropriate properties are set

text(minX - HorizontalOffset, yTicks(yy) - VerticalOffset, ['$$\begin{array}{c}',num2str( yTicks(yy)),'\\',num2str( 2*yTicks(yy)),'\end{array}$$'], 'Interpreter', 'latex')
% {c} specifies that the elements of the different lines will be center

% aligned. It may be replaced by {l} or {r} for left or right alignment

end
%%Reset the XTicklabels onto multiple lines, 2nd line being twice of first
minY = min(yTicks);
% You will have to adjust the offset based on the size of figure
VerticalOffset = 0.6;
HorizontalOffset = 0.2;
for xx = 1:length(xTicks)
% Create a text box at every Tick label position
% String is specified as LaTeX string and other appropriate properties are set
text(xTicks(xx) - HorizontalOffset, minY - VerticalOffset, ['$$\begin{array}{c}',num2str( xTicks(xx)),'\\',num2str( 2*xTicks(xx)),'\end{array}$$'], 'Interpreter', 'latex')
% {c} specifies that the elements of the different lines will be center
% aligned. It may be replaced by {l} or {r} for left or right alignment
end