How to display info with mouse over text region inside a figure

조회 수: 50 (최근 30일)
Ole
Ole 2017년 8월 10일
댓글: Stephen Bradshaw 2021년 6월 17일
Is there a way to display information when you put the mouse over a text region inside a figure. *
x = linspace(-3,3);
y = (x/5-x.^3).*exp(-2*x.^2);
plot(x,y)
text(0,0,'text*')
When the mouse is over the text to display 'some additional info'
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 8월 10일
Not easily. However you can add a ButtonDownFcn callback to cause something to happen on left click, or a uicontextmenu to present a menu on right click.

댓글을 달려면 로그인하십시오.

답변 (3개)

Josh
Josh 2017년 8월 17일
Actually, it's not hard at all. MATLAB's pointer manager lets you assign functions to be called whenever the mouse pointer enters, crosses (traverses) or exits any graphics object. I included a short example where "text*" is changed to a longer string when you mouse over it. In principle, you can define behavior as complicated as you like.
% ======================================================================= %
% Your code with a few additions:
% Save the figure handle for later
hf = figure;
x = linspace(-3,3);
y = (x/5-x.^3).*exp(-2*x.^2);
plot(x,y);
% Save the text handle and give it a tag to make it easier to find...
ht = text(0,0,'text*', 'tag', 'rollover');
% ======================================================================= %
% Define a pointer behavior struct containing the fields 'enterFcn',
% 'exitFcn', and 'traverseFcn'. These fields should contain function
% handles or an empty matrix ([]). MATLAB will call these functions when
% the mouse pointer first moves over an object ('enterFcn'), when the mouse
% pointer moves off of an object ('exitFcn') and when the mouse pointer
% moves across and object ('traverseFcn'). MATLAB passes two arguments when
% it calls these functions: the figure handle, and the cursor position.
% I've defined the enterFcn to find the object with the 'rollover' tag (the
% text box) and change it's string value to something more informative...
pointerBehavior.enterFcn = ...
@(hfig, cpp)set(findobj(hfig, 'tag', 'rollover'), ...
'string', 'text - here''s some more info...');
% I don't care about what happens when the mouse moves across the text, so
% I'll leave the traverseFcn blank...
pointerBehavior.traverseFcn = [];
% The exitFcn is similar to the enterFcn, but it changes the string back to
% the shorter version...
pointerBehavior.exitFcn = ...
@(hfig, cpp)set(findobj(hfig, 'tag', 'rollover'), ...
'string', 'text*');
% Now, I need to link the pointer behavior to the object (the text box):
iptSetPointerBehavior(ht, pointerBehavior);
% Now, I need to enable pointer management for the figure:
iptPointerManager(hf, 'enable');
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 8월 17일
Interesting; I had not seen this before.
I notice that iptSetPointerBehavior is part of the Image Processing Toolbox, rather than being generally available.

댓글을 달려면 로그인하십시오.


Mathias
Mathias 2020년 2월 12일
There is a ToolTip property on uicontrols uitables etc. (ToolTipStr for older versions).

HJP Kuykens
HJP Kuykens 2021년 2월 3일
Unfortunately the proposed tool is currently only available in the image processing toolbox. Matlab, why?

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by