How to enable the data tip interaction functionality when using a custom "ButtonDownFcn" in MATLAB R2023a?

7 views (last 30 days)
I am using MATLAB R2023a, and I have a custom "ButtonDownFcn" to display the point clicked in the plot.
x = linspace(0,2*pi,100);
y = sin(x);
p = plot(x,y);
xlabel('x');
ylabel('sin(x)');
% Custom ButtonDownFcn to display point clicked
set(p,'ButtonDownFcn',{@buttondownfcn}); 
function buttondownfcn(~,event)
    pts = event.IntersectionPoint(1:2);
    disp(pts);
end
However, I notice that when using a custom "ButtonDownFcn", I no longer have the default interactivity of a plot (e.g., displaying data tip upon hovering, etc.). How can I enable the data tip interaction functionality when using a custom "ButtonDownFcn"?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 26 Mar 2024
Edited: MathWorks Support Team on 26 Mar 2024
Currently, the "ButtonDownFcn" callback does not have the functionality to enable data tip interaction. To enable the data tip interaction functionality in MATLAB R2023a, consider using the "Hit" event instead of a "ButtonDownFcn". Please see the following code snippet for an example.
x = linspace(0,2*pi,100);
y = sin(x);
p = plot(x,y);
xlabel('x');
ylabel('sin(x)');
% Add a Hit event listener
addlistener(p,'Hit',@(src,event)buttondownfcn(src,event));
function buttondownfcn(~,event)
    pts = event.IntersectionPoint(1:2);
    disp(pts);
end

More Answers (0)

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!