Matlab GUI 中多Axes下的鼠标动态跟踪
windowbuttonmotionfcn 函数
文章来源:不详 作者:佚名
--------------------------------------------------------------------------------
该文章讲述了Matlab GUI 中多Axes下的鼠标动态跟踪windowbuttonmotionfcn 函数.
在GUI 中建立多个Axes用于同时显示不同的曲线或图像,并通过鼠标在各个Axes间进行交互操作是Matlab GUI编程的常见需求。下面的代码用于实现这种效果。
代码的基本原理是运用figure控件所支持的回调函数(callback function):windowbuttonmotionfcn。其作用是检测到figure上有鼠标按下时(无论左右键)即发生调用,执行windowbuttonmotionfcn内所设计的代码。
下面的例子建立了一个带有windowbuttonmotionfcn回调函数的实例,具体功能如下:
当GUI中某个Axes上发生鼠标按键时,则该函数开始实时记录鼠标所在坐标,并实时显示在设定的静态文本框内。
代码如下:
function CurPosShow(OpSet)
% Show (On/Off) the Cursor Position on the Current Axes
% OpSet = 'on' or 'off'
% the axes tag is '**n' ,and textbox' tag should be 'TXn' and 'TYn'
% Version 1.0Bata
% CopyRight WEI Zhen 2010-2014
handles=guidata(gcbo); % get handles
X=get(gca,'XLim'); % x range of current axes
Xmin=X(1);
Xmax=X(2);
Y=get(gca,'YLim'); % y range of current axes
Ymin=Y(1);
Ymax=Y(2);
AxName=get(gca,'Tag'); % name of the current axes
hTX=findobj(gcf,'tag',['TX' AxName(end)]); % find correspond text box x
hTY=findobj(gcf,'tag',['TY' AxName(end)]); % find correspond text box y
%
set(gcf,'windowbuttonmotionfcn',''); % dissable all callback in figure
set(gcf,'windowbuttonmotionfcn','CurPosShow on'); % set callback when mouse move
switch OpSet
case 'on'
curPos = get(gca, 'CurrentPoint');
pX=max([Xmin curPos(1,1)]);
pX=min([Xmax pX]);
pY=max([Ymin curPos(1,2)]);
pY=min([Ymax pY]);
set(hTX,'String',pX);
set(hTY,'String',pY);
case 'off'
set(gcf,'windowbuttonmotionfcn',''); % dissable callback
end
guidata(gcbo,handles); % save handles (refresh)
以上函数要求Axes的名称为‘[Axes]n’的形式,而对应的用于分别显示x,y坐标的文本框命名为 TX
来源: 本文来自: 高校自动化网(Www.zdh1909.com) 详细出处参考(转载请保留本链接):http://www.zdh1909.com/html/matlab/15737.html