MATLAB: How to add XTickLabels in a subplot (2014a)

2014asubplotxticklabels

I am having difficulty applying XTickLabels to a subplot. Many examples I find online do not seem to work or give errors. By moving around when the axes are defined I can get the TickLabels on a normal figure, but in a subplot the same order doesn't give labels AND messes up the other labels. Any thoughts?
%%title and labels ok, no tick labels
xlabel('x');
ylabel('y');
title('subplot (1,1)');
axes('XTickLabel',{'a','b','c'});
%%everything ok
axes('XTickLabel',{'a','b','c'});
xlabel('x');
ylabel('y');
title('subplot (1,1)');
%%title and labels ok, no tick labels on subplot1
subplot(3,3,1);
xlabel('x');
ylabel('y');
title('subplot (1,1)');
axes('XTickLabel',{'a','b','c'});
subplot(3,3,2);
xlabel('x');
ylabel('y');
title('subplot (1,2)');
%%No labels for subplot 1, labels ok for subplot 2
subplot(3,3,1);
axes('XTickLabel',{'a','b','c'});
xlabel('x');
ylabel('y');
title('subplot (1,1)');
subplot(3,3,2);
xlabel('x');
ylabel('y');
title('subplot (1,2)');

Best Answer

I think that part of the problem is that when the code calls axes or xlabel it applies the ticks or labels to the current axis which may not be the one corresponding to your subplot. I took the middle chunk of code from above (starting at %% title and labels ok, no tick labels on subplot1) and put it in a test.m file and stepped through each line and observed the changes to the subplots as the line was executed. It may be interesting to do the same.
Now in order to apply the ticks and labels to your choice of subplot, you need the handle to the axis of that subplot. That is easy:
h = subplot(3,3,1);
Then to set the title, labels and ticks, do something like
title(h,'subplot (1,1)');
xlabel(h,'x');
ylabel(h,'y');
set(h,'XTickLabel',{'a','b','c'});
And repeat for the other subplots. Try the above and see what happens!
Related Question