Hovering on an Graphics Object

3 ビュー (過去 30 日間)
Rightia Rollmann
Rightia Rollmann 2017 年 2 月 27 日
回答済み: Adam 2017 年 2 月 27 日
I want the red rectangle to turn green while I'm hovering the mouse over it. How?
hax = axes('XLim', [0 4], 'YLim', [0 4]);
r = rectangle('Position', [1 1 1 1], 'FaceColor', 'r');


Adam 2017 年 2 月 27 日
As a rough guide:
axesPos = getpixelposition( hax );
rectXPos = [1 2] / 4 * axesPos(3) + axesPos(1);
rectYPos = [1 2] / 4 * axesPos(4) + axesPos(2);
Then you will need to use the
of the figure and create a function that tests the figure's
property against those rectXPos and rectYPos to see if you are inside the rectangle or not. The callback syntax will need to be something like
hFig.WindowButtonMotionFcn = @(src,evt) motionFunc( src, rectXPos, rectYPos );
and a function syntax e.g.
funcion motionFunc( hFig, rectXPos, rectYPos )
currentPoint = hFig.CurrentPoint;
% Do the obvious maths here of cyrrentPoint(1) against rectXPos and currentPoint(2) against rectYPos
I've probably missed out one or two things as I don't have time to give a full answer, but you should get the idea. Obviously the hard coded numbers in the recXPos and rectYPos maths should be replaced by calculations based on your rectangle position and axes limits rather than hard-coded.
If you allow zooming then these will need to be calculated dynamically because the XLim and YLim will change.

その他の回答 (0 件)


Find more on Graphics Object Properties 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!

Translated by