function [x,y]=pdesnap(ax,pv,flag)
%PDESNAP Returns new x,y coord's which are "snapped" to the closest grid point.
%	[X,Y]=PDESNAP(AX,PV,FLAG) 'snaps' the current position PV to the
%	closest grid point in the axes with handle AX, if FLAG is set
%	(the default).

%	Magnus Ringh 5-26-94, MR 11-11-94.
%	Copyright (c) 1994-95 by The MathWorks, Inc.
%	$Revision: 1.3.1.2 $  $Date: 1995/09/01 08:23:19 $

if nargin<3, flag=1, end
xticks=get(ax,'XTick');
yticks=get(ax,'YTick');
limits=get(ax,'renderlimits');
if isempty(xticks) | isempty(yticks) | flag==0,
  x=pv(1,1); y=pv(1,2);
else
  x=xticks(find(min(abs(xticks-pv(1,1)))==abs(xticks-pv(1,1))));
  x=x(1);
  y=yticks(find(min(abs(yticks-pv(1,2)))==abs(yticks-pv(1,2))));
  y=y(1);
end
x=sort([limits(1:2) x]); x=x(2);
y=sort([limits(3:4) y]); y=y(2);
