MATLAB Answers

0

Mouseover highlighting of plotted line?

Asked by Jurgens Wolfaardt on 12 Jul 2019
Latest activity Commented on by cliffy
on 3 Aug 2019
The 'parallelplot' function has a very handy mouseover function that highlights the proximate line in the plot and displays its parameters automatically in a datatip. I had to write my own parallel coordinate plot function due to parallelplot's limited color control, line sorting and label control but the mouseover feature is a bridge too far for me. Can anyone please give me direction to accomplish this for the plain 'plot' function?
Thanks!

  0 Comments

Sign in to comment.

1 Answer

Answer by Abhilash Padma on 19 Jul 2019
 Accepted Answer

Hi,
I understand that you wanted to implement the mouseover functionality for plotted lines.
MATLAB's pointer manager lets you assign functions to be called whenever the mouse pointer enters, traverses or exits any graphics object. I included an example here where a line changes to another color when mouse passes through that line.
% ======================================================================= %
% 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);
hp=plot(x,y, 'tag', 'rollover','color','black');
% ======================================================================= %
% 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'), ...
'color', 'red');
% 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'), ...
'color', 'black');
% Now, I need to link the pointer behavior to the object (the text box):
iptSetPointerBehavior(hp, pointerBehavior);
% Now, I need to enable pointer management for the figure:
iptPointerManager(hf, 'enable');
% function changeColor(hfig, cpp)
% set(findobj(hfig, 'tag', 'rollover'), ...
% 'color', 'red');
% end
Please refer to the iptSetPointerBehavior documentation page for more information.

  2 Comments

Thank you Abhilash!
Hi Abhilash,
I'm using the pointerBehavior function. It works, it can show some more text when I move my cursor to a textbox in the figure. I wonder if you have encounter the problem of several objects i.e. when move the cursor over different objects, different text will show up. Thank you! And does the object have to be a textbox, or could it be some plots?

Sign in to comment.