function [figax, theaxes]=multiax(m, n, limits)
%%multiax.m
%
% [figax, theaxes]=multax(m, n, limits)
%
%Returns handles to m*n axes in the same figure where m is 
%the number of rows and n is the number of columns.
%The handle figax is to an axes containing most of the
%figure (i.e. for labelling the whole window).  The 
%remaining axes (theaxes) are numbered down columns starting 
%from the upper left corner.   Example: n=m=3
%
%           1	4   7
%           2	5   8
%           3	6   9
%
%If desired, you may control the spacing of the axes by passing the
%optional vector limits.  The meaning and default of the entries in
%limits are :
%
%          limits(1) = topbuff    = .12;   space at top
%                (2) = botbuff    = .1;    space at bottom
%                (3) = leftbuff   = .09;   space at left
%                (4) = rightbuff  = .07;   space at right
%                (5) = Xbuff      = .06;   space between the columns
%                (6) = Ybuff      = .06;   space between the rows

if (nargin>4 | nargin<2)
	error('You must include 2 or 3 inputs: n, m and possibly limits(1, 6)');
end;
if (nargin==3)
	if(length(limits)~=6)
		error('Limits must contain 6 values 0<1');
	end;
	topbuff=limits(1);
	botbuff=limits(2);
	leftbuff=limits(3);
	rightbuff=limits(4);
	Xbuff=limits(5);
	Ybuff=limits(6);
else
	topbuff=.12;		%space at top
	botbuff=.1;		%space at bottom
	leftbuff=.09;		%space at left
	rightbuff=.07;		%space at right
	Xbuff=.06;		%space between the columns
	Ybuff=.06;		%space between the rows
end;
f=gcf;


Height=1/m*(1-(topbuff+botbuff+(m-1)*Ybuff));
Width=1/n*(1-(leftbuff+rightbuff+(n-1)*Xbuff));

figax(1)=axes('Position', [0,0,1,1]);
set(figax, 'Visible', 'off');

i=1;

for j=1:n
    for k=1:m;
	X=leftbuff+(j-1)*(Width+Xbuff);
	Y=botbuff+(m-k)*(Height+Ybuff);
	theaxes(i)=axes('Position', [X, Y, Width, Height]);
	i=i+1;
    end;
end;
