Matlab GUI 中多Axes下的鼠标动态跟踪[windowbuttonmotionfcn 函数]
来源:互联网 发布:.wang域名能备案吗 编辑:程序博客网 时间:2024/06/05 10:51
在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坐标的文本框命名为 TXn 和TYn
例如,GUI上有两个Axes,分别命名为 Pic1和Pic2,那么每个对应的坐标显示框则应当命名为:
Pic1 对应 TX1 和 TY1
Pic2 对应 TX2 和 TY2
主程序中对该函数的调用非常简单,例如在Pic1上用callback向导生成ButtonDownFcn回调函数,调用时直接写为:
% --- Executes on mouse press over axes background.
function Pic1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to Pic1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
CurPosShow('on');
当程序工作是,鼠标点中Pic1框后,即可发现其位置坐标反映在TX1和TY1中了。坐标值实时的在随着鼠标位置变化,无需反复点击,点击一次即可。
继续对Pic1加载图片或曲线时,以上代码单击Pic1框会失效,无法显示坐标,并报错。这是因为imshow函数的设置问题,更改办法如下:
[FileName,PathName] = uigetfile('*.jpg','Select File');
path=fullfile(PathName,FileName);
img=imread(path);
axes(handles.Pic1);
set(gca,'NextPlot','replacechild');
hImg=imshow(img);
handles.hImg=hImg;
set(hImg,'ButtonDownFcn',{@ImgButtonDown,handles});
*重要步骤
1. 读入图片后,选择Pic1为当前Axes界面: axes(handles.Pic1);
2. 设置当前Axes界面的‘NextPlot’属性为'replacechild': set(gca,'NextPlot','replacechild');
3. 设置基于image的“buttonDown”回调函数(而不是之前属于Axes callback的Pic1_ButtonDownFcn)
set(hImg,'ButtonDownFcn',{@ImgButtonDown,handles});
4.在ImgButtonDown 函数中 如下书写:
function ImgButtonDown(hObject, eventdata, handles)
CurPosShow('on');
到此大功告成,效果如下:
- Matlab GUI 中多Axes下的鼠标动态跟踪[windowbuttonmotionfcn 函数]
- Matlab GUI 中多Axes下的鼠标动态跟踪
- MATLAB中axes函数
- MATLAB中GUI的使用技巧(1)之axes
- 关于matlab GUI 中 多个plot(handles.axes) 无法hold on的问题
- Matlab中axes函数解析
- Matlab的GUI中figure的WindowButtonDownFcn与axes的ButtonDownFcn
- 关于matlab的gui中调获取axes里data cursor的坐标位置
- MATLAB GUI axes的ButtondownFCN无效的问题
- matlab中axes用法
- Matlab axes的使用
- GUI中axes使用subplot时的清空及axes大小的固定的解决方法
- java中gui实现跟踪鼠标
- matlab gui(二)对象(axes,line[ezplot,plot],回调函数的写法)
- Matlab常用函数axes和axis
- Matlab GUI鼠标画线
- MATLAB GUI中Edit Text的Callback函数何时执行
- MATLAB中GUI的handles
- C# 根据经度纬度计算两点的距离
- tcp/ip Incoming Connection Request Queue(呼入连接请求队列)
- 一个简单的 .Net Vim 引擎 LibNVim 开发记录 (三)
- android 利用VideoView播放视频文件
- javaCode解密系列---深入java类加载器1
- Matlab GUI 中多Axes下的鼠标动态跟踪[windowbuttonmotionfcn 函数]
- The day
- AT91SAM9260不能与sam-ba连接的解决方法
- 网络搜索引擎大全
- 书和耳机到了
- 数据结构基础(待完善)
- [转贴]IPOD接口定义
- C语言的一些可能犯迷糊的基础
- Android权限获取机制与常见权限不足问题分析