MATLAB Answers

Ole
1

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

Ole
さんによって質問されました 2017 年 8 月 10 日
最新アクティビティ Walter Roberson
さんによって コメントされました 2017 年 8 月 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
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.

サインイン to comment.

1 件の回答

回答者: 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
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.

サインイン to comment.



Translated by