移动图中的多个盒子?
我已经拥有在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通常使用
findall
而不是findobj
,以防从外部看不到对象的句柄。除此之外,我不明白为什么你的代码不起作用。这是一个例子:
I usually use
findall
instead offindobj
, 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:
正如乔纳斯提到的,
'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.你应该反转 x 和 y 向量的顺序,并且你可以只使用一个循环,代码中的变化是:
you should inverse the ordre of x and y vector, and you can use just one loop, the changment in your code is :