function AxesPos = axispos(Ny,Nu,RefAxes,scale);
%AXISPOS  Computes desirable positions for a Ny-by-Nu grid of axis
%
%   AxesPos is a cell array of size (Ny,Nu) where each cell contains a 
%   valid row vector axis position.

%   Revised: Adam DiVergilio, 11-99
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.8 $  $Date: 2000/06/14 20:07:53 $

if nargin<4
   scale = 1;
end

%---Get reference axes position in pixels
OldUnit = get(RefAxes,'Units');
if strcmpi(OldUnit,'pixels')
   position = get(RefAxes,'Position');
else
   oldpos = get(RefAxes,'Position');
   set(RefAxes,'Units','pixels');
   position = get(RefAxes,'Position');
   set(RefAxes,'Units',OldUnit,'Position',oldpos);
end

%---Adjusted RefAxes position (accounts for IO labels)
tshave = 20*scale;
yshave = 12*scale;
position = position + [yshave 0 -yshave -tshave];

%---Spacing between adjacent axes
xsp = 16*scale;  %---Leave enough space for so tick labels don't overlap too much
ysp = 16*scale;
SWH = (position(3:4) - [Nu-1 Ny-1].*[xsp ysp])./[Nu Ny];

%---Make sure axes width and height are at least 1 pixel
if any(SWH<1)
   SWH = max([1 1]*scale,SWH);
   disp('Warning: Figure too small to accurately display plot axes!');   
end

%---Calculate axes positions
AxesPos = cell(Ny,Nu);
for ctU=1:Nu
   for ctY=1:Ny
      AxesPos{ctY,ctU} = [(position(1:2)+[ctU-1 Ny-ctY].*(SWH+[xsp ysp])) SWH];
   end
end
