function subplotmx(x,y)

%SUBPLOTMX Remove white spaces between subplots in an existing
%  Matlab subplot graph by moving axes on top of and adjacent to each
%  other.  
%
%  SUBPLOTMX(X,Y) compresses a subplot graph with X rows and Y columns.
%  Tick labels are removed as follows:
%      - x labels are removed from all but the bottom row
%      - y labels are removed from all but the left-most column
%
% Doug Brungart 1-98
% Comments & minor modifications by D. Tran 11-1-99

n = y*x;       % number of subplots

for k=1:n;   % cycles through all subplots
   evalstr=['subplot(',num2str(x),',',num2str(y),',', num2str(k), ')'];
   eval(evalstr);
   h=gca;
   P(k,:)=get(h,'position'); % get axes position [lft bot width height]
end

xspace=(P(1,2)+P(1,4)-P(n,2))/x;    % total height inc. white space
yspace=(P(n,1)+P(n,3)-P(1,1))/y;    % total width inc. white space 

P(:,4)=(xspace)*ones(y*x,1);	    % change height to total height
P(:,3)=(yspace)*ones(y*x,1);	    % change width to total width

% adjust left coordinates for plots in 2nd column, 3rd column, etc.
for j=2:y;
   P(j:y:n,1)=(P(1,1)+yspace*(j-1))*ones(x,1);
end;

% adjust bottom coordinates for rows 1, 2, ..., n-1
for j=0:x-2;
   P(j*y+1:(y*(j+1)),2)=(P(n,2)+xspace*(x-j-1))*ones(y,1);
end;

for k=1:n;
   evalstr=['subplot(',num2str(x),',',num2str(y),',', num2str(k), ')'];
   eval(evalstr);
   h=gca;
   set(h,'position',P(k,:));	    % set the new coordinates

   if (k<=y*(x-1))  % do this for all but the last row
       if (x>1)	    % do this only if more than one row
	   set(h,'XTickLabel',[]);  % kill x-labels
       end;
   end;

   if (rem(k,y)~=1) % do this only for all but the 1st column
       if (y>1)	    % do this only if more than 1 column
	   loc = get(gca,'YAxisLocation');
	   if (~strcmp(loc,'right'))    % if labels not on right side
	       set(h,'YTickLabel',[]);  % kill y-labels
	   end
       end;
   end;

end;





