【问题标题】:Getting current position of one of the multiple objects in a figure?获取图中多个对象之一的当前位置?
【发布时间】:2011-09-25 21:26:07
【问题描述】:

我写了一个脚本,它返回一个图中的几个文本框。文本框是可移动的(我可以拖放它们),它们的位置由输入矩阵中的数据预先确定(输入矩阵中的数据通过嵌套的 for 循环应用于框的相应位置)。我想创建一个矩阵,它最初是输入矩阵的副本,但在我通过拖动它们来更改框的位置时已更新。我将如何更新他们的职位?这是整个脚本

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';
        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'}

【问题讨论】:

  • 究竟如何更新文本框的位置?
  • @Itamar Katz :由于文本框“位置”的属性由 4 个元素 [xy 宽度高度] 组成,新的 x 和 y 是我的目标是“更新”或记录在先前规定盒子应该去哪里的矩阵。我的目标是通过get(gco,'Position)
  • 你写了I change the positions of the boxes by dragging them around,所以在你更新盒子位置的任何回调中,你也可以更新矩阵,不是吗?还是我误解了你的问题?
  • @Itamar Katz:确实如此——我已经尝试这样做了一段时间。我有三个启用移动的主要功能,它们将先前的位置与对象被拖动到的位置进行比较。我现在需要找到从这些函数中提取信息的最佳方法。你理解了这个问题。
  • 很抱歉,我仍然不能 100% 理解您的问题。如果你能放一些代码并告诉你到目前为止你尝试了什么,它会有所帮助。

标签: arrays user-interface matlab position


【解决方案1】:

如果我理解正确(如果我没有正确理解,请发布一些代码),那么您所需要的确实是一个 set/get 组合。

如果boxHandle 是文本框对象的句柄,那么您可以通过以下方式获取其当前位置:

pos = get (boxHandle, 'position')

其中pos 是[x, y, width, height] 的输出数组。 为了设置到一个新的位置,你使用:

set (boxHandle, 'position', newPos)

其中newPos 是所需位置的数组(与pos 具有相同的结构)。

编辑

关于更新矩阵,由于您拥有移动对象的句柄,因此您实际上可以访问特定的文本框。

当您创建每个文本框时,设置一个名为“UserData”的属性以及用于该框的tsinput 的关联索引。在你的嵌套 for 循环中添加这个

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

创建盒子后,在你的moveObject回调中获取数据

udata = get(dragging,'UserData');

然后udata 包含您要更新的元素的索引。

【讨论】:

  • 我的主要问题是启用移动的函数处理“当前”对象,这意味着我能获得的唯一数据是以数字 x 和 y 的形式表示位置,但未附加到任何特定文本盒子。我现在会尝试发布大部分脚本
  • 卡茨:是的,这是有道理的。我在想相反的方向。感谢您的回答。
猜你喜欢
  • 2016-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-23
  • 1970-01-01
  • 2019-02-06
  • 2011-07-15
  • 2018-10-27
相关资源
最近更新 更多