|
以下内容摘自好友lyqmath新浪博客,在此借花献佛。
前言 相比于VC++的MFC界面平台,个人感觉MATLAB的鼠标点击响应有点麻烦。这里介绍一种简单的处理方式,其核心依然是响应鼠标单击回调函数的编写。
代码- % By lyqmath
- % DLUT School of Mathematical Sciences 2008
- % BLOG:http://blog.sina.com.cn/lyqmath
- function main()
- clc; close all;
- global hfig
- hfig = figure;
- hold on; box on;
- haxis = gca;
- x = linspace(0, 2*pi);
- y1 = sin(x);
- y2 = cos(x);
- line(y1, y2);
- axis equal;
- set(haxis, 'ButtonDownFcn', @click_ceshi);
- function click_ceshi(src, event)
- global hfig
- % 获取当前点坐标
- xy = get(hfig, 'CurrentPoint');
- % 获取gcf和gca的位置信息
- hpos = get(hfig, 'Position');
- apos = get(gca, 'Position');
- % 更新点的信息,获取点针对于gca的准确坐标信息
- x = (xy(1) - apos(1)*hpos(3))/(apos(3)*hpos(3));
- y = (xy(2) - apos(2)*hpos(4))/(apos(4)*hpos(4));
- xlim = get(gca, 'XLim');
- ylim = get(gca, 'YLim');
- x = x*(xlim(2) - xlim(1)) + xlim(1);
- y = y*(ylim(2) - ylim(1)) + ylim(1);
- % 标注
- text(x, y, 'Happy', 'color', rand(3, 1));
复制代码 结果
|
|