Setting ButtonDownFcn disables default datatip functionality on hover

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 comentarios

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');
Hi Johannes,
Thanks for your reply!
It is a bit annoying indeed.
That has been my workaround for now, did not know the datacursormode function though, used to do it from the fig itself. Thanks for the tip!
Would be great if someone can explain to us why does this happen. I looked around to see how does Matlab internally enables the data tips on hover by default, but couldn't find something useful.
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
Did you manage to solve this issue? I am having the same problem and I just can't find the solution...
No, I manually swap between the two modes by selecting/deselecting the Datatips feature in the figure itself.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Productos

Versión

R2019a

Preguntada:

el 15 de Abr. de 2020

Comentada:

el 17 de Jun. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by