recipes : Plotting : Plot Manipulation : Add axis labels only to peripheral sub-plots

Problem

You have a grid of sub-plots, all of which plot data along the same axes. To save space, you only want to label the axes on the plots along the left edge and the bottom.

Solution

There are multiple solutions to this problem. For example, if you are making the sub-plots within a loop then you can use if statements to check if the current sub-plot is one which be labeled. You may not always use loops, however. So a more a elegant solution is initially label all your plots then use an external function to remove the unwanted labels. The following is a simple implementation of this.

%First make some plots
for ii=1:9
   subplot(3,3,ii)
   plot(randn(1,60),'-k')
   xlabel('X Axis')
   ylabel('Y Axis')
   xlim([1,60])
end

%The following code has the effect of removing the unwanted axis
%labels. Ultimately you would palce this in a separate function
%to allow for re-use. 

c=get(gcf,'children'); %get the axes
%Find the axis labels (since all are the same)
xlab= get(get(c(1),'xlabel'),'string');
ylab= get(get(c(1),'ylabel'),'string');

%Remove any currently existing axis labels, noting the location
%of each axis
pos=ones(length(c),4);
for i=1:length(c)
     pos(i,:)=get(c(i),'position');
     set(get(c(i),'xlabel'),'string','')
     set(get(c(i),'ylabel'),'string','')
end


%Add the labels back to the edge plots
for ii=1:length(c)
    %Add X 
    if pos(ii,2)==min(pos(:,2));
        set(get(c(ii),'xlabel'),'string',xlab)
    end
    %Add Y
    if pos(ii,1)==min(pos(:,1));
        set(get(c(ii),'ylabel'),'string',ylab)
    end
end


Discussion

The above code is somewhat long-winded but it has flexibility. Here is a fully-fledged function based on the above solution, it contains error checking and allows the user to define the axis labels: labelEdgeSubPlots.m