Setting ButtonDownFcn disables default datatip functionality on hover
Show older comments
Hello,
Matlab nicely has the builtin feature of viewing the data tips when the mosue hovers on a point in a plot.
However, when I try to set the ButtonDownFcn of the plot, this default data tip functionality is disabled.
Is there a way to have both: View data points on hover and fire a function when the plot is clicked?
Example:
figure()
a=plot(1:10,1:10)
%Figure now shows data tips perfectly
set(a,'HitTest', 'on', 'ButtonDownFcn',@(varargin)disp('Anything'))
%Running the last line disables the datatips on hovering to enable firing the disp function when plot is clicked
Many thanks!
5 Comments
Johannes Hougaard
on 16 Apr 2020
Hi Ahmad
This is really annoying - I realized the exact same thing when creating a buttondownfcn to identify a given curve in plot of many lines.
Some of the functionality can be restored by calling
datacursormode(gcf,'on');
...it still doesn't allow you to view datatips on hovering, but you can disable your ButtonDownFcn for a while for setting a dataip and then restore it afterwards with.
datacursormode(gcf,'on');
Ahmad Khaled
on 16 Apr 2020
Michael Hotto
on 14 Dec 2021
Edited: Michael Hotto
on 14 Dec 2021
I have the same issue, adding a simple ButtonDownFcn disables default mouse hover highlighting and datatips.
When comparing s1 to s2, the only property difference I could identify was the ButtonDownFcn field which is what I would expect.
Johannes's answer before me makes it so you have to physically deselect "Data Tips" in the figure interactivity menu in order for a button down function to work on a selected point. You will also have to deselect "Data Tips" if you want any of the other figure interactivities to work.
Example:
function interactive_sin()
% tested on MATLAB 2021b
X = 0:pi/100:2*pi;
Y = sin(X);
h = figure();
%s1 = scatter(X,Y,'filled'); -> has default MATLAB hover highlighting & datatips
s2 = scatter(X,Y,'filled','ButtonDownFcn',{@selected_pt,h}); % -> does NOT have default MATLAB hover highlighting & datatips
end
% -------------------------------------------------------------------------
function selected_pt(src,event,h)
pt = event.IntersectionPoint;
fprintf('You selected pt: [%0.2f %0.2f]\n',pt(1),pt(2))
% do more stuff with selected pt here but removed from this quick example for simplicity:
%
%
%
end
Jonatha Reis
on 17 Jun 2022
Did you manage to solve this issue? I am having the same problem and I just can't find the solution...
Ahmad Khaled
on 17 Jun 2022
Answers (0)
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!