function clipline(gcax,ii_interp)
%
% Call clipline(gca)
%
% Example of how manually clip lines to the axes in MATLAB.
%
% Note: If the line does not intersect the axes at a point 
% defined in the X, Y, or Z data, then the line is
% not clipped at the axes.  It is clipped relative to
% the data.
%
% To properly clipthe line relative to the data, you
%     have to use interpolation.  This was not included in the
%     example.
%
% DISCLAIMER:  This M-file was developed for my own personal use.
%            It has not been tested, nor is it supported by The
%            MathWorks, Inc.  Please feel free to use and modify
%            this program to suit your needs.

% Written by John L. Galenski III - Aug. 31, 1995
% Copyright(c) By The MathWorks, Inc, 1995
% All Rights reserved
% LDM083195jlg

% Get axis limits
if nargin<1, gcax=gca; end
if nargin<2, ii_interp=1; end
ax = axis;
axis(ax) % Freeze limits so they do not change when 
 %printed/resized

% EKW: pass axes handle (gca) instead of children's handle:
handle=get(gcax,'Children');
if ii_interp==0,
% Clip the line
	for i = 1:length(handle)
		h = handle(i); type=get(h,'type');
		if ~strcmp(type,'text'),
			Xd = get(h,'XData');
			Yd = get(h,'YData');
	
% Test range
			Xm = (Xd<ax(1) | Xd>ax(2));
			Xd(Xm) = nan * ones(1,sum(Xm));
			Ym = (Yd<ax(3) | Yd>ax(4));
			Yd(Ym) = nan * ones(1,sum(Ym));

% Update plot
			set(h,'XData',Xd,'YData',Yd)
			if length(ax)==6
				Zd = get(h,'ZData');
				Zm = (Zd<ax(5) | Zd>ax(6));
				Zd(Zm) = nan * ones(1,sum(Zm));
				set(h,'ZData',Zd)
			end
		end
	end
else
	for i = 1:length(handle)
		h = handle(i); type=get(h,'type');
		if ~strcmp(type,'text'),
			Xd = get(h,'XData');
			Yd = get(h,'YData');
			[Xd,Yd]=clip_interp(Xd,Yd,ax(1),1);
			[Xd,Yd]=clip_interp(Xd,Yd,ax(2),2);
			[Yd,Xd]=clip_interp(Yd,Xd,ax(3),1);
			[Yd,Xd]=clip_interp(Yd,Xd,ax(4),2);
% Update plot
			set(h,'XData',Xd,'YData',Yd)
			if length(ax)==6
				[Zd,Xd]=clip_interp(Zd,Xd,ax(5),1);
				[Zd,Xd]=clip_interp(Zd,Xd,ax(6),2);
				set(h,'ZData',Zd)
			end
		end
	end
end
%
function [Xd2,Yd2]=clip_interp(Xd,Yd,Xlim,iilo);
%
Xd2=Xd(:); Yd2=Yd(:);
lXd=length(Xd);
if iilo==1, iix = find(Xd2<Xlim); else, iix=find(Xd2>Xlim); end
liix=length(iix); if liix==0, break; end
% Find contiguous groups:
diix=diff(iix); iig=find(diix>1)+1;
iig1=[1; iig]; iig2=[iig-1; liix]; ngroup=length(iig1);
for jg=ngroup:-1:1,
	iig=iig1(jg):iig2(jg); liig=length(iig);
	n1=iix(iig1(jg)); n2=iix(iig2(jg));
	if n1==1, n1x=[n1 n1+1]; n1nan=n1; else, n1x=[n1-1,n1]; n1nan=n1+1; end
	if n2==lXd, n2x=[n2-1 n2]; n2nan=n2; else, n2x=[n2,n2+1]; n2nan=n2-1; end
	if diff(Xd(n1x))==0, 
		't', %keyboard, 
	end
	Yclip1=mylinear(Xd(n1x),Yd(n1x),Xlim,0);
	Xclip1=Xlim;
	Yclip2=mylinear(Xd(n2x),Yd(n2x),Xlim,0);
	Xclip2=Xlim;
	if n2nan>=n1nan, Xd2(n1nan:n2nan)=NaN; end
	
	if n2>n1+1,
		Xd2(n1)=Xclip1; Yd2(n1)=Yclip1;
		Xd2(n2)=Xclip2; Yd2(n2)=Yclip2;
	else
% Case where only one or two points lie outside limit: need to insert a point
		Xd2=[Xd2(1:n1-1); Xclip1; NaN; Xclip2; Xd2(n2+1:lXd)];
		Yd2=[Yd2(1:n1-1); Yclip1; Yclip1; Yclip2; Yd2(n2+1:lXd)];
	end
end

	
%
% COPYRIGHT (c) 2008
% 
% Applied Research Laboratories, The University of Texas at Austin
% (ARL:UT)
% All rights reserved.
% 
% This material may be reproduced by or for the U.S. Government
% only. This notice must appear in all copies of this file and its
% derivatives. Further distribution outside the U.S. Government is
% prohibited without the explicit written authorization from the
% Director of the Environmental Sciences Laboratory, ARL:UT.
% 
% THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE
% IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
% PURPOSE, OR NONINFRINGEMENT.
% 
% IN NO EVENT WILL ARL:UT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING
% OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN IF ARL:UT HAS
% BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
