Detect mouse position over embedded plot in live script.

33 views (last 30 days)
I just started using live scripts. Now I am trying to figure out if I can detect mouse position over an embedded plot.
I tried using callback functions and events with the embedded figures, without much luck so far. Here's a non-working example of what I am trying to do:
hfig = figure();
plot(axes('Parent', hfig), rand(5));
hfig.WindowButtonMotionFcn = @(src,evt)disp(src);
This displays a plot inline and I was hoping that when I move the mouse pointer over the plot, the `src` object would be printed out. That does not happen. If I click 'Open in figure window' and then move the mouse over the figure, the callback starts firing.
Is there any way to get callback functions or other events to to work in live scripts. Or is there some other way to read out mouse position in embedded plots?

Answers (1)

Ravi
Ravi on 4 Oct 2023
Hi DNF,
According to my understanding, you have a plot on which you want to record the cursor position as your mouse pointer is hovered on the figure.
The figure object has a callback function, “WindowButtonMotionFcn” that triggers when the cursor is hovered on the figure.
Now the objective is to print the position of the cursor every time it is moved. The axes object has a property, “CurrentPoint” that gives the current position of the cursor. The callback function can be written in such a way that the position is printed using the “CurrentPoint” property of the axes object.
For more information, kindly refer to the below links.
Hope this helps.

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!