Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在《matlab指南》中,当鼠标悬停在图像上时,如何显示不同的信息?_Matlab_Tooltip_Matlab Guide - Fatal编程技术网

在《matlab指南》中,当鼠标悬停在图像上时,如何显示不同的信息?

在《matlab指南》中,当鼠标悬停在图像上时,如何显示不同的信息?,matlab,tooltip,matlab-guide,Matlab,Tooltip,Matlab Guide,我有一个图像,它由几个带不同标签的片段组成。标签是彩色编码的。有人知道当我将鼠标悬停在matlab上时,如何让matlab在文本框中显示当前标签吗? 我基本上需要根据鼠标光标在图像上的位置显示不同的文本框 我发现了一些使用工具提示的示例,但这些示例仅适用于按钮,而不适用于图像,具体取决于鼠标悬停的确切像素位置 下面是一个示例图像: 我不是100%清楚您所说的“几个标签”是什么意思,但我认为您希望能够在图像上显示弹出式编辑,我认为以下内容可以满足您的要求: function hoverTest

我有一个图像,它由几个带不同标签的片段组成。标签是彩色编码的。有人知道当我将鼠标悬停在matlab上时,如何让matlab在文本框中显示当前标签吗? 我基本上需要根据鼠标光标在图像上的位置显示不同的文本框

我发现了一些使用
工具提示的示例,但这些示例仅适用于按钮,而不适用于图像,具体取决于鼠标悬停的确切像素位置

下面是一个示例图像:
我不是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
功能测试
%创建一个图形(单位规格化使得在回调中更容易更新位置)
f=数字(‘单位’、‘标准化’);
%创建一个轴
ax=轴(‘父’,f);
%加载一些用于打印的数据
c=荷载(“小丑”);
%绘制数据
图像(c.X,‘父’,ax);
%创建一个uipanel来承载我们将显示的文本
uip=uipanel('parent',f',position',[0.18 0.05],'visible','off');
%创建文本控件以显示图像信息
txt=uicontrol('style','edit','parent',uip',units','normalized','position',[0 0 1]);
%指定鼠标移动时的回调
f、 WindowButtonMotionFcn=@(对象、事件)更新信息(f、uip、ax、txt、c.X);
结束
函数updateInfo(f、uip、ax、txt、img)
%更新uipanel的位置-基于当前地物点(标准化单位)
设置(uip,‘位置’,[f.CurrentPoint uip.位置(3:4)];
%检查图形是否位于轴上方(如果未隐藏)
如果ax.CurrentPoint(1,1)max(ax.XLim)|。。。
最大电流点(1,2)最大值(ax.YLim)
uip.Visible='off';
其他的
%显示面板
uip.Visible='on';
%获取当前点
cp=圆形(最大电流点);
%更新uicontrol的文本字符串
String=sprintf('img(%i,%i)=%i',cp(1,1),cp(1,2),img(cp(1,2),cp(1,1));
结束
结束

您是否希望显示的信息在鼠标移动时跟随鼠标?或者在GUI中的某个固定位置,或者在每个段中居中?两者都可以。信息应该只取决于鼠标当前悬停在上面的图像的(x,y)位置。似乎可以工作,谢谢:)。虽然我有MatlabR2013A,所以我必须对每个对象显式调用
handle()
,以获得其句柄。例如
ax=handle(ax)
实际上,您还知道如何使用一个小的黄色文本框(如工具提示)来显示其中的文本,还是不可能?一个简单的方法就是将txt控件的背景色设置为黄色。您的示例运行良好。但是,我使用的是matlab指南,如上图所示。我无法创建新的图形、轴。。。您知道如何在matlab guide中获得相应的句柄吗?我从不使用guide-但所有内容都应该在句柄结构中-可能是
handles.figure
handles.axes
。您仍然可以创建其他
uicontrols
来创建工具提示。