【问题标题】:Moving multiple boxes in figure?在图中移动多个框?
【发布时间】:2011-07-12 20:14:40
【问题描述】:

我已经拥有在 MATLAB 中拖放单个框所需的功能。我编写的代码用几个框填充了该图。在另一个循环中,我在图中填充了更多的框(以字符串形式保存不同的信息)。

这两组框由我在它们的 UserData 中放置的数字相关(对应的数字;对于每个框,都有另一个具有相同 UserData 内容的框)。通过查找包含相同 UserData 的框(并因此关联它们),我希望能够通过右键单击将第一组框的成员重新定位到相对于第二组框的相应成员的相同位置我刚刚拖动的框(uicontextmenu)。

function recallfcn(hObject,eventdata)
for ydx=1:2
    diag_detail=get(gco,'UserData');   % This line should be in the drag fcn
    diag_pos=get(gco,'Position');      % So should this one (for current objects)
    xvar=diag_pos(1,1);
    yvar=diag_pos(1,2);
    detail=[diag_detail ydx]; 
    set(findobj('UserData',detail),'Position',[xvar+(ydx-1.5) yvar+0.5 0.8 0.8]);
end
end

% ydx is only there to add another level of detail as I'm actually looking to move     
% two boxes of the 'first kind', each of which have 2 numbers in user data, the first  
% number being the same, and the second number distinguishing the first box from the 
% second. The premise is the same.

【问题讨论】:

  • 是的,它应该像您描述的那样工作。那么问题是什么?
  • 当我右键单击一个框时,我正在使用它的手柄。一旦我移动了第二组盒子的一个成员,我就使用函数 findobj(.) ,它应该返回另一个盒子的句柄(我想要相对于我提到的第一个盒子重新定位的盒子)。由于使用了两个句柄,并且代码什么也不做 - 也没有返回任何错误消息,因此似乎存在某种冲突。我将编辑问题以包含一些我希望用于重定位的代码。

标签: matlab drag-and-drop figure


【解决方案1】:

我通常使用findall 而不是findobj,以防从外面看不到对象的句柄。除此之外,我不明白为什么您的代码不起作用。

这是一个例子:

%# make a figure with two buttons, same userData
fh=figure,
uicontrol('userdata',[2 3],'parent',fh)
uicontrol('userData',[2 3],'units','normalized','position',[0.5 0.5,0.1 0.1],'parent',fh)

%# change color to red
set(findall(fh,'userData',[2 3]),'backgroundcolor','r')

%# move to the same position
set(findall(fh,'userData',[2 3]),'position',[0.3,0.3,0.1,0.1])

【讨论】:

    【解决方案2】:

    As Jonas alludes to,对象的'HandleVisibility' property 将确定该对象是否出现在其父级的子级列表中,因此它是否会被FINDOBJ 之类的函数返回。标准的解决方法是改用函数FINDALL

    然而,'HandleVisibility' property 在确定一个对象是否可以成为current object 时也发挥了作用(即可由函数 GCO 返回)。如果设置为'off',则该对象不能成为当前对象。此外,如果对象的父图形'HandleVisibility' property 设置为'off',则其所有子对象(包括所述对象)都不能成为当前对象。

    如果所有对象和图形的'HandleVisibility' 设置为'on''callback',那么我认为一切正常。

    【讨论】:

    • 谢谢,解释和 Jonas 的例子帮助我更多地了解了 MATLAB 中的句柄。
    【解决方案3】:

    你应该反转 x 和 y 向量的顺序,你可以只使用一个循环,你的代码中的变化是:

    x2=x(end:-1:1); % invers the ordre
    y2=y(end:-1:1);
    
    for i=1:length(x)
    
    set(hLine,'xdata',x(i),'ydata',y(i)); % move the point using set
                                      % to change the cooridinates.
    
    set(hLine2,'xdata',x2(i),'ydata',y2(i));
    
     M(i)=getframe(gcf);
    
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 2014-04-26
      • 2019-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多