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

52 ビュー (過去 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 Center および File ExchangeGraphics Object Programming についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by