4

I have an image that consists of several segments with different labels. The labels are color coded. Does someone know how I can tell matlab to display the current label in a text box when hovering over it? I would basically need to display different text boxes depending on where on the image the mouse cursor is placed.

I found some examples using tooltip but these apply only to buttons and not to images depending on the exact pixel location that the mouse is hovering over.

Here is an example image: enter image description here

mcExchange
  • 6,154
  • 12
  • 57
  • 103
  • Do you intend the displayed information to follow the mouse as it moves? Or in a fixed location somewhere in the GUI, or centred in each segment? – Will Jul 18 '16 at 09:29
  • Both would be fine. The info should just depend on the (x,y) position of the image that the mouse is currently hovering above. – mcExchange Jul 18 '16 at 09:52

1 Answers1

3

I'm not 100% clear on what you mean by "several labels", but I think you want to be able to display a popup edit over an image, I think the following does what you want:

function hoverTest
  % create a figure (units normalized makes updating the position easier in the callback
  f = figure ( 'units', 'normalized' );
  % create an axes
  ax = axes ( 'parent', f );
  % load some data for plotting
  c = load ( 'clown' );
  % plot the data
  image ( c.X, 'parent', ax );
  % create a uipanel to host the text we will display
  uip = uipanel ( 'parent', f, 'position', [0 0 0.18 0.05], 'visible', 'off' );
  % create a text control to display the image info
  txt = uicontrol ( 'style', 'edit', 'parent', uip, 'units', 'normalized', 'position', [0 0 1 1] );
  % assign the callback for when the mouse moves
  f.WindowButtonMotionFcn =  @(obj,event)updateInfo(f,uip,ax,txt,c.X);
end
function updateInfo ( f, uip, ax, txt,img )
  % update the position of the uipanel - based on the current figure point (normalized units)
  set ( uip, 'Position', [f.CurrentPoint uip.Position(3:4)] );
  % Check to see if the figure is over the axes (if not hide)
  if ax.CurrentPoint(1,1) < min(ax.XLim) || ...
     ax.CurrentPoint(1,1) > max(ax.XLim) || ...
     ax.CurrentPoint(1,2) < min(ax.YLim) || ...
     ax.CurrentPoint(1,2) > max(ax.YLim)
   uip.Visible = 'off';
  else
    % show the panel
    uip.Visible = 'on';
    % get the current point
    cp = round(ax.CurrentPoint);
    % update the text string of the uicontrol
    txt.String = sprintf ( 'img(%i,%i) = %i', cp(1,1), cp(1,2), img(cp(1,2),cp(1,1) ) );
  end

end
matlabgui
  • 5,642
  • 1
  • 12
  • 15
  • Seems to work, thank you :). Although I have Matlab R2013a so I have to call `handle()` explicitly on every object in order to get its handle. Eg `ax = handle(ax)` – mcExchange Jul 19 '16 at 08:58
  • Actually do you also know how to use a small yellow textbox (like tooltip) to display the text in it or is that not possible ? – mcExchange Jul 19 '16 at 09:17
  • A simple way is just to set the background color of the txt control to be yellow. – matlabgui Jul 19 '16 at 09:20
  • Your example is working fine. However I'm using matlab guide, as shown in the picture above. I cannot create new figures, axes... Do you know how I can get the corresponding handles within matlab guide? – mcExchange Jul 19 '16 at 09:39
  • I never use guide - but everything should be in your handles structure - probably `handles.figure` and `handles.axes`. You can still create other `uicontrols` to create the tooltip though. – matlabgui Jul 19 '16 at 09:51