Matlab:如何使用回调获取单击时的当前鼠标位置 [英] Matlab: How to get the current mouse position on a click by using callbacks

查看:18
本文介绍了Matlab:如何使用回调获取单击时的当前鼠标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在近处和远方进行了搜索,但无法获得有关如何将回调与 matlab 中的点击事件关联的示例.有人可以给我举个例子吗?

解决方案

使用 set 命令和 @callbackfunction 标签定义图形回调的 WindowButtonDownFcn.>

像这样:

function mytestfunction()f=数字;set(f,'WindowButtonDownFcn',@mytestcallback)函数 mytestcallback(hObject,~)pos=get(hObject,'CurrentPoint');disp(['你点击了 X:',num2str(pos(1)),', Y:',num2str(pos(2))]);

您还可以使用单元格符号将额外的变量传递给回调函数:

set(f,'WindowsButtonDownFcn',{@mytestcallback,mydata})

如果您正在使用 uicontrol 对象,则是:

set(myuicontrolhandle,'Callback',@mytestcallback)

I googled near and far, but couldn't get an example of how you associate a callback to the click event in matlab. Can someone show me an example?

解决方案

Define the WindowButtonDownFcn of your figure callback using the set command and an @callbackfunction tag.

Like so:

function mytestfunction()
f=figure;
set(f,'WindowButtonDownFcn',@mytestcallback)

function mytestcallback(hObject,~)
pos=get(hObject,'CurrentPoint');
disp(['You clicked X:',num2str(pos(1)),', Y:',num2str(pos(2))]);

You can also pass extra variables to callback functions using cell notation:

set(f,'WindowsButtonDownFcn',{@mytestcallback,mydata})

If you're working with uicontrol objects, then it's:

set(myuicontrolhandle,'Callback',@mytestcallback)

这篇关于Matlab:如何使用回调获取单击时的当前鼠标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆