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

46 次查看(过去 30 天)
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 中查找有关 Interactive Control and Callbacks 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by