在 matlab 指南中用鼠标悬停在图像上时如何显示不同的信息?
How to display different infos when hovering over an image with mouse in matlab guide?
我有一张图片,其中包含多个带有不同标签的片段。标签采用颜色编码。有人知道我如何告诉 matlab 在悬停时在文本框中显示当前标签吗?
我基本上需要根据鼠标光标在图像上放置的位置显示不同的文本框。
我找到了一些使用 tooltip
的示例,但这些示例仅适用于按钮而不适用于图像,具体取决于鼠标悬停在其上的确切像素位置。
这是一个示例图片:
我不是 100% 清楚你所说的 "several labels" 是什么意思,但我认为你希望能够在图像上显示弹出式编辑,我认为以下内容可以满足你的要求:
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
我有一张图片,其中包含多个带有不同标签的片段。标签采用颜色编码。有人知道我如何告诉 matlab 在悬停时在文本框中显示当前标签吗? 我基本上需要根据鼠标光标在图像上放置的位置显示不同的文本框。
我找到了一些使用 tooltip
的示例,但这些示例仅适用于按钮而不适用于图像,具体取决于鼠标悬停在其上的确切像素位置。
这是一个示例图片:
我不是 100% 清楚你所说的 "several labels" 是什么意思,但我认为你希望能够在图像上显示弹出式编辑,我认为以下内容可以满足你的要求:
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