function theAxis = xpsubplt(nrows, ncols, thisPlot)
%XPSUBPLT Create axes in tiled positions.
% 	XPSUBPLT(m,n,p) is the version of SUBPLOT for use with 
%	Expo files. It is faster than SUBPLOT and assumes nargin = 3.

%	Copyright (c) 1984-94 by The MathWorks, Inc.

narg = nargin;
create_axis = 1;
delay_destroy = 0;
kill_siblings = 1;

% if we haven't recovered position yet, generate it from mnp info:
	if (thisPlot < 1)
		error('Illegal plot number.')
        elseif (thisPlot > ncols*nrows)
                error('Index exceeds number of subplots.')
	else
 	% This is the percent offset from the subplot grid of the plotbox.
                PERC_OFFSET_L = 0.09;
                PERC_OFFSET_R = 0.045;
                PERC_OFFSET_B = PERC_OFFSET_L;
                PERC_OFFSET_T = PERC_OFFSET_R;
                if nrows > 2
                        PERC_OFFSET_T = 0.9*PERC_OFFSET_T;
                        PERC_OFFSET_B = 0.9*PERC_OFFSET_B;
                end
                if ncols > 2
                        PERC_OFFSET_L = 0.9*PERC_OFFSET_L;
                        PERC_OFFSET_R = 0.9*PERC_OFFSET_R;
                end

		row = (nrows-1) -fix((thisPlot-1)/ncols);
		col = rem (thisPlot-1, ncols);

% For this to work the default axes position must be in normalized coordinates
		def_pos = get(gcf,'DefaultAxesPosition');
		totalwidth = def_pos(3);
		totalheight = def_pos(4);
		width = (totalwidth - (ncols-1)*(PERC_OFFSET_L+PERC_OFFSET_R))/ncols;
		height = (totalheight - (nrows-1)*(PERC_OFFSET_T+PERC_OFFSET_B))/nrows;
		position = [def_pos(1)+col*(width+PERC_OFFSET_L+PERC_OFFSET_R) ...
			    def_pos(2)+row*(height+PERC_OFFSET_T+PERC_OFFSET_B) ...
			    width height];
		if width <= 0.5*totalwidth/ncols
			position(1) = def_pos(1)+col*(totalwidth/ncols);
			position(3) = 0.7*(totalwidth/ncols);
		end
		if height <= 0.5*totalheight/nrows
			position(2) = def_pos(1)+row*(totalheight/nrows);
			position(4) = 0.7*(totalheight/nrows);
		end
	end

% kill overlapping siblings if mnp specifier was used:
nextstate = get(gcf,'nextplot');
if strcmp(nextstate,'replace'), nextstate = 'add'; end
	if delay_destroy, set(gcf,'NextPlot','replace'); return, end
	sibs = get(gcf, 'Children');
	for i = 1:max(size(sibs))
		if(strcmp(get(sibs(i),'Type'),'axes'))
			units = get(sibs(i),'Units');
			set(sibs(i),'Units','normalized')
			sibpos = get(sibs(i),'Position');
			set(sibs(i),'Units',units);
			intersect = 1;
			if(     (position(1) >= sibpos(1) + sibpos(3)) | ...
		                (sibpos(1) >= position(1) + position(3)) | ...
               			(position(2) >= sibpos(2) + sibpos(4)) | ...
		                (sibpos(2) >= position(2) + position(4)))
	               		 intersect = 0;
			end
			if intersect
				if any(sibpos ~= position)
					delete(sibs(i));
				else
					set(gcf,'CurrentAxes',sibs(i));
                                        if strcmp(nextstate,'new')
						create_axis = 1;
					else
						create_axis = 0;
					end
				end
			end
		end
	end
	set(gcf,'NextPlot',nextstate);

% create the axis:
if create_axis
	if strcmp(nextstate,'new'), figure, end
	ax = axes('units','normal','Position', position);
        set(ax,'units',get(gcf,'defaultaxesunits'))
else 
	ax = gca; 
end
