簡體   English   中英

在Matlab指南中用鼠標懸停在圖像上時如何顯示不同的信息?

[英]How to display different infos when hovering over an image with mouse in matlab guide?

我有一個包含幾個帶有不同標簽的片段的圖像。 標簽采用顏色編碼。 有人知道我如何將鼠標懸停在matlab上告訴matlab在文本框中顯示當前標簽嗎? 我基本上需要根據鼠標光標放在圖像上的位置顯示不同的文本框。

我發現了一些使用tooltip示例,但這些示例僅適用於按鈕,不適用於圖像,具體取決於鼠標懸停在其上的確切像素位置。

這是一個示例圖像: 在此處輸入圖片說明

對於“幾個標簽”的含義,我還不是100%清楚,但我認為您希望能夠在圖像上顯示彈出式編輯,我想以下是您想要的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM