views:

606

answers:

1

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?

+3  A: 

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

related questions