Detect mouse position over embedded plot in live script.

32 次查看(过去 30 天)
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?

回答(1 个)

Ravi
Ravi 2023-10-4
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.

类别

Help CenterFile Exchange 中查找有关 Graphics Object Properties 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by