Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 766
olamundo Avatar asked May 04 '10 21:05

olamundo


1 Answers

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)
like image 71
Doresoom Avatar answered Oct 02 '22 01:10

Doresoom