MATLAB Answers

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

86 views (last 30 days)
Ole
Ole on 10 Aug 2017
Answered: HJP Kuykens on 3 Feb 2021
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 Comment
Walter Roberson
Walter Roberson on 10 Aug 2017
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.

Sign in to comment.

Answers (3)

Josh
Josh on 17 Aug 2017
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 Comment
Walter Roberson
Walter Roberson on 17 Aug 2017
Interesting; I had not seen this before.
I notice that iptSetPointerBehavior is part of the Image Processing Toolbox, rather than being generally available.

Sign in to comment.


Mathias
Mathias on 12 Feb 2020
There is a ToolTip property on uicontrols uitables etc. (ToolTipStr for older versions).

HJP Kuykens
HJP Kuykens on 3 Feb 2021
Unfortunately the proposed tool is currently only available in the image processing toolbox. Matlab, why?

Community Treasure Hunt

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

Start Hunting!

Translated by