www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/@cgsurfview/@contourplot/setCursorMode.m

    function setCursorMode(obj,newstate,cursorcallback)
%CONTOURPLOT/SETCURSORMODE Sets the cursor mode state and its callback
%
% setCursorMode(obj,newstate,cursorcallback)
%
% The state can be "on", "off" or "inactive".
% The "src" and "evt" parameters of the callback will be
%   the contourplot object
%   the string to be shown

%  Copyright 2000-2013 The MathWorks, Inc. and Ford Global Technologies, Inc.


if nargin>2
    obj.CursorCallback = cursorcallback;
end

switch newstate
case 'on'
    set(obj.hAxes,'ButtonDownFcn',{@i_axes_click, obj});
case 'inactive'
    if ~isempty(obj.hCursorLines) && all(isgraphics(obj.hCursorLines))
        set(obj.hCursorLines,'Color',[0.8 0.8 0.8]);
    end
case 'off'
    delete(obj.hCursorLines);
    obj.hCursorLines = [];
    set(obj.hAxes,'ButtonDownFcn',[]);
end

%%%%%%%%%%%%%%%%%%%%%%%
function i_axes_click(src,evt,obj)

if isempty(obj.hCursorLines) || ~any(isgraphics(obj.hCursorLines))
    obj.hCursorLines = [...
        line('Parent',obj.hAxes, 'XData',[],'YData',[],'LineWidth',2);...
        line('Parent',obj.hAxes, 'XData',[],'YData',[],'LineWidth',2) ];
    set(obj.hCursorLines,'ButtonDownFcn',...
        {@i_line_click,obj});
else
    set(obj.hCursorLines,'Color',[0 0 0])
end
i_ShowMousePosition([],[],obj);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function i_ShowMousePosition(ignoresrc, ignoreevt, obj)

% Find the coordinates of the clicked point
cp = get(obj.hAxes,'CurrentPoint');
x = cp(1,1);
y = cp(1,2);
% Interpolate to find the Z-value at this point
zdata = getValues(obj.PlotData,1);
z = interp2(obj.xdata',obj.ydata',zdata',x,y);
% Generate a string describing the click
str = sprintf('%s: %s = %f, %s = %f, %s = %f',...
    getTitle(obj.PlotData),...
    getVariableName(obj.PlotData,1),... % xname
    x,...
    getVariableName(obj.PlotData,2),... % yname
    y,...
    getDatasetName(obj.PlotData,1),...
    z);
% Set the string back to the application
xregcallback(obj.CursorCallback,obj,str); % src, evt
% Calculate the best position for the crosshairs
xrange = get(obj.hAxes,'XLim');
yrange = get(obj.hAxes,'YLim');
dx = (xrange(2) - xrange(1) ) / 50;
dy = (yrange(2) - yrange(1) ) / 50;
% Set the crosshair position.
set(obj.hCursorLines(1),'XData',[x-dx x+dx],'YData',[y-dy y+dy]);
set(obj.hCursorLines(2),'XData',[x-dx,x+dx],'YData',[y+dy y-dy]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%
function i_line_click(src,evt,obj)

upfcn = get(gcbf,'WindowButtonUpFcn');
motionfcn = get(gcbf,'WindowButtonMotionFcn');
set(gcbf,'WindowButtonMotionFcn',{@i_ShowMousePosition, obj});
set(gcbf,'WindowButtonUpFcn',{@i_release,obj});
set(obj.hCursorLines,'Color',[0 0 0])
i_ShowMousePosition([],[],obj);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function i_release(src,evt,obj)

set(gcbf,'WindowButtonMotionFcn',[]);
set(gcbf,'WindowButtonUpFcn',[]);
i_ShowMousePosition([],[],obj);