Using WindowButtonDownFcn callback to track mouse moves in figure
Show older comments
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)
Geoff Hayes
on 8 Jun 2016
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
Geoff Hayes
on 8 Jun 2016
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?
Geoff Hayes
on 8 Jun 2016
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
Geoff Hayes
on 8 Jun 2016
Luc's answer moved here
Yes, the figure is created programmatically.
Thanks for the answer.
Rik
on 3 Nov 2017
If you are not nesting functions, you can use guidata as well to access the handles struct.
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!