Matlab: How to get the current mouse position on a click by using callbacks

限于喜欢 提交于 2019-12-17 10:49:31

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/2769249/matlab-how-to-get-the-current-mouse-position-on-a-click-by-using-callbacks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!