开发者

Getting current position of one of the multiple objects in a figure?

I wrote a script that returns several text boxes in a figure. The text boxes are moveable (I can drag and drop them), and their positions are predetermined by the data in an input matrix (the data from the input matrix is applied to the respective positions of the boxes by nested for loop). I want to create a matrix which is initially a copy of the input matrix, but is UPDATED as I change the positions of the boxes by dragging them around. How would I update their positions? Here's the entire script

function drag_drop=drag_drop(tsinput,infoinput)
[x,~]=size(tsinput);
dragging = [];
orPos = [];
fig = figure('Name','Docker Tool','WindowButtonUpFcn',@dropObject,...
'units','centimeters','WindowButtonMotionFcn',@moveObject,...
'OuterPosition',[0 0 25 30]);
% Setting variables to zero for the loop
plat_qty=0;
time_qty=0;
k=0;
a=0; 
% Start loop
z=1:2
    for idx=1:x
        if tsinput(idx,4)==1
            color='red';
        else
            color='blue';
      开发者_如何学Go  end
        a=tsinput(idx,z);
       b=a/100;
        c=floor(b); % hours
        d=c*100;
        e=a-d; % minutes
        time=c*60+e; % time quantity to be used in 'position'
        time_qty=time/15;
        plat_qty=tsinput(idx,3)*2;
        box=annotation('textbox','units','centimeters','position',...
        [time_qty plat_qty 1.5 1.5],'String',infoinput(idx,z),...
        'ButtonDownFcn',@dragObject,'BackgroundColor',color); 
        % need to new=get(box,'Position'), fill out matrix OUT of loop
    end
    fillmenu=uicontextmenu;
    hcb1 = 'set(gco, ''BackgroundColor'', ''red'')';
    hcb2 = 'set(gco, ''BackgroundColor'', ''blue'')';
    item1 = uimenu(fillmenu, 'Label', 'Train Full', 'Callback', hcb1);
    item2 = uimenu(fillmenu, 'Label', 'Train Empty', 'Callback', hcb2);
    hbox=findall(fig,'Type','hggroup');
    for jdx=1:x
        set(hbox(jdx),'uicontextmenu',fillmenu);
    end
end
new_arr=tsinput;

function dragObject(hObject,eventdata)
dragging = hObject;
orPos = get(gcf,'CurrentPoint');
end

function dropObject(hObject,eventdata,box)
if ~isempty(dragging)
    newPos = get(gcf,'CurrentPoint');
    posDiff = newPos - orPos;
    set(dragging,'Position',get(dragging,'Position') + ...
    [posDiff(1:2) 0 0]);
    dragging = [];
end
end

function moveObject(hObject,eventdata)
if ~isempty(dragging)
newPos = get(gcf,'CurrentPoint');
posDiff = newPos - orPos;
orPos = newPos;
set(dragging,'Position',get(dragging,'Position') + [posDiff(1:2) 0 0]);
end
end
end

% Testing purpose input matrices:
% tsinput=[0345 0405 1 1 ; 0230 0300 2 0; 0540 0635 3 1; 0745 0800 4 1]
% infoinput={'AJ35 NOT' 'KL21 MAN' 'XPRES'; 'ZW31 MAN' 'KM37 NEW' 'VISTA';
% 'BC38 BIR' 'QU54 LON' 'XPRES'; 'XZ89 LEC' 'DE34 MSF' 'DERP'}


If I understand you correctly (and please post some code if I'm not), then all you need is indeed a set/get combination.

If boxHandle is a handle to the text-box object, then you get its current position by:

pos = get (boxHandle, 'position')

where pos is the output array of [x, y, width, height]. In order to set to a new position, you use:

set (boxHandle, 'position', newPos)

where newPos is the array of desired position (with the same structure as pos).

EDIT

Regarding to updating your matrix, since you have the handle of the object you move, you actually DO have access to the specific text box.

When you create each text box, set a property called 'UserData' with the associated indices of tsinput used for that box. In your nested for loop add this

set (box, 'UserData', [idx, z]);

after the box is created, and in your moveObject callback get the data by

udata = get(dragging,'UserData');

Then udata contains the indices of the elements you want to update.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜