【发布时间】: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