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-98 by The MathWorks, Inc.
%       $Revision: 1.7 $  $Date: 1997/11/25 17:06:28 $

if nargin<3, flag=1, end
xticks=get(ax,'XTick');
yticks=get(ax,'YTick');
xlim=get(ax,'XLim');
ylim=get(ax,'YLim');
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([xlim x]); x=x(2);
y=sort([ylim y]); y=y(2);

