Using WindowButtonDownFcn callback to track mouse moves in figure

In Matlab 2013a I must track mouse moves in a figure to let user position vertical markers (timeline limits).
After creating my figure with hfig = figure('Position',rect, ...
I call set(hfig,'WindowButtonDownFcn',@ButtonDown);
and declare
function ButtonDown(hObject, eventdata, handles)
But when I click the mouse in the figure, I get "Undefined function 'ButtonDown' for input arguments of type 'double'. Error while evaluating figure WindowButtonDownFcn".
What is the right function prototype for this callback?

Answers (1)

Luc - the correct signature for this function would be
function ButtonDown(hObject, eventdata)
since you are not supplying any additional parameters when you assign this callback to the WindowsButtonDownFcn
set(hfig,'WindowButtonDownFcn',@ButtonDown);
However, I suspect that isn't the source of the error. Where is this function, ButtonDown, defined relative to your function that assigns it as a callback? Are they both in the same file?

4 Comments

Luc's answer moved here
Both functions were in same .m file, but the callback was defined after its invocation. I fixed the prototype and moved it up and it now works. Thanks!
If I want to have access to handles in the callback, what must I change in the invocation?
Are you using GUIDE to create your GUI or are you programmatically creating a GUI? I think the latter and so handles must be a local variable in your main script. If so, then you can nest ButtonDown callback in your main function so that it has access to the local variables defined there. For example,
function myMainFunc
% some code
% create the figure
handles.hFig = = figure('Position',rect, ...);
% assign the callback
set(hfig,'WindowButtonDownFcn',@ButtonDown);
% nest the callback
function ButtonDown(hObject, eventdata)
% make use of handles
get(handles.hFig); % or whatever
end
end
Luc's answer moved here
Yes, the figure is created programmatically.
Thanks for the answer.
If you are not nesting functions, you can use guidata as well to access the handles struct.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Asked:

on 8 Jun 2016

Commented:

Rik
on 3 Nov 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!