移动图中的多个盒子?

发布于 2024-11-19 11:56:45 字数 962 浏览 3 评论 0原文

我已经拥有在 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.

I already have the functions required to drag and drop a single box in a figure in MATLAB. The code I wrote fills the figure with several boxes. With another loop I filled the figure with more boxes (which hold different information in string form).

These two sets of boxes are related by the numbers I placed in their UserData (corresponding numbers; for each box, there's another with the same UserData content). By finding boxes containing the same UserData (and thus relating them) I want to be able to relocate a member of the first set of boxes to the same position relative to the corresponding member of the second set of boxes, by means of right clicking on the box I just dragged (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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里人 2024-11-26 11:56:45

我通常使用 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])

I usually use findall instead of findobj, in case the handles of the objects are not visible from the outside. Other than that I don't see why your code wouldn't work.

Here's an example:

%# 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])
岁月静好 2024-11-26 11:56:45

正如乔纳斯提到的'HandleVisibility' 对象的属性将确定该对象是否显示在其父级的子级列表中,从而确定它是否由 FINDOBJ。标准修复方法是使用函数 FINDALL 代替。

但是,'HandleVisibility' 属性 还在确定对象是否可以成为 当前对象(即可以由函数GCO返回)。如果将其设置为'off',则该对象不能成为当前对象。此外,如果 'HandleVisibility' 属性<对象的父图形的/a>设置为'off',则其子对象(包括所述对象)都不能成为当前对象。

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

As Jonas alludes to, the 'HandleVisibility' property of an object will determine if the object shows up in its parent's list of children, and thus if it will be returned by functions like FINDOBJ. The standard fix is to use the function FINDALL instead.

However, the 'HandleVisibility' property also comes into play in determining whether or not an object can become the current object (i.e. returnable by the function GCO). If it is set to 'off', then that object can't become the current object. Additionally, if the 'HandleVisibility' property of the parent figure of an object is set to 'off' then none of its children (including said object) can become the current object.

If 'HandleVisibility' is set to 'on' or 'callback' for all your objects and figures, then I think everything should work fine.

话少心凉 2024-11-26 11:56:45

你应该反转 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

you should inverse the ordre of x and y vector, and you can use just one loop, the changment in your code is :

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文