MATLAB Answers

How can I execute a double-click callback without executing the single-click callback in my MATLAB GUI?

119 views (last 30 days)
In order to set a double-click callback for my figure, I must set the SelectionType property to "open." However, when I double-click on my figure, MATLAB cycles the SelectionType through "normal" while I am clicking. This triggers my single-click callback. Thus both callbacks are executed instead of only my double-click callback.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 Dec 2011
You can use the following sample code to cause your application to execute only the double-click callback:
Associate this function with 'WindowButtonDownFcn' callback of the figure window.
function clickcallback(obj,evt)
persistent chk
if isempty(chk)
chk = 1;
pause(0.5); %Add a delay to distinguish single click from a double click
if chk == 1
fprintf(1,'\nI am doing a single-click.\n\n');
chk = [];
chk = [];
fprintf(1,'\nI am doing a double-click.\n\n');
  1 Comment
Alon Rozen
Alon Rozen on 21 Dec 2016
Cool !!
Works nicely on axes which don't have the 'SelectionType' property and thus cannon distinguish between one and double click.
Thanks :)

Sign in to comment.

More Answers (3)

CAM on 26 Mar 2015
I am disappointed that GUI's do not (apparently) support the double-click option in SelectionType. Why is there no disclaimer in the help files about this?
When will this bug be fixed?
  1 Comment
Christoph on 27 Mar 2015
But you can use SelectionType, for example
function example
close all
function clickcallback(obj,evt)
switch get(obj,'SelectionType')
case 'normal'
case 'open'
disp('double click')

Sign in to comment.

Nick Patterson
Nick Patterson on 25 Aug 2020
My answer, which I made before seeing this, is very similar to the first one, but it doesn't burn a half-second each time:
%% Constants:
persistent click_time
click_threshold_s = 0.5;
%% Later in code, after sorting by source type (or whatever you need):
if isempty(click_time)
click_time = tic();
time_between_clicks = toc( click_time );
click_time = tic();
if time_between_clicks < click_threshold_s
% Enter desired callback / action here.
end % if double-click
end % if empty
For my case, I take no action on a single click, but you could make a separate action there if desired.




Community Treasure Hunt

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

Start Hunting!