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));

结果

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐