开发者

Moving multiple boxes in figure?

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',开发者_JAVA技巧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 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])


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜