带有 addlisteners 的多个 GUI 点
Multiple GUI points with addlisteners
我有一个 GUI,用户可以在其中单击一个按钮来创建一个点。此点用于测量(即计算它与另一个静态点之间的距离)。
function pushbutton1_Callback(hObject, eventdata, handles)
a = handles.calciter; % This iterates for each time pushbutton is pressed.
mypoint = drawpoint(handles.axes1);
handles.el = addlistener(mypoint, 'ROIMoved', @mypointmoved_Callback);
因此,如果我的 pushbutton1
创建另一个点;我怎样才能得到它,所以每个点都有多个 addlistener
?
我需要多个 handles.el
吗?
如果有兴趣,我的 包含更多信息。
继续我之前的回答...
- 为每个新点添加一个监听器。
- 你不需要有多个
handles.el
,你可以忽略el
,因为你根本不用它。
- 您可以存储创建点的句柄数组(如果需要)。
- 您可以将点的索引存储在
mypoint.UserData
中(如果需要的话)。
- 在监听器的回调中,可以从数组中获取任意一个点,也可以查看触发事件的点的索引。
这里是代码的相关部分(请阅读评论):
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
axes(handles.axes1);
mypoint = drawpoint;
%Add event listenr to every point
%if ~isfield(handles, 'el')
addlistener(mypoint, 'ROIMoved', @mypointmoved_Callback);
%end
%Save an array of handles to created points:
if ~isfield(handles, 'mypoints')
mypoint.Color = [0.7410, 0.2, 0]; %First point has a different color.
handles.mypoints = mypoint; %Add first point.
%Distance from the first added point.
handles.distx = 0;
handles.disty = 0;
set(handles.edit1, 'String', '0'); %Display the distance from the first added point.
else
handles.mypoints(end+1) = mypoint; %Add a new point to the end of the array
end
points_counter = length(handles.mypoints); %Number of points - use as "Point serial number".
mypoint.UserData = points_counter; %Store the index of the point in UserData property
% Update handles structure
guidata(hObject, handles);
function mypointmoved_Callback(src, eventData)
handles = guidata(src.Parent); %Get the handles using the parent of the point (the axes).
if src.UserData == 1
%Example for using the index of the point stored in UserData property.
set(handles.edit1, 'String', '0');
return;
end
px = src.Position(1);
py = src.Position(2);
%Get the first point that added by the user
p0 = handles.mypoints(1);
%Get the x,y position of the first point that added by the user
p0x = p0.Position(1);
p0y = p0.Position(2);
%Compute the distance from p0:
distx = abs(px - p0x);
disty = abs(py - p0y);
handles.distx = distx;
handles.disty = disty;
dist = norm([distx, disty]); %Euclidean distance
%Display the euclidean distance from p0.
set(handles.edit1, 'String', num2str(dist));
drawnow
该示例显示用户移动的点到第一个(用户)添加的点的欧式距离。
我有一个 GUI,用户可以在其中单击一个按钮来创建一个点。此点用于测量(即计算它与另一个静态点之间的距离)。
function pushbutton1_Callback(hObject, eventdata, handles)
a = handles.calciter; % This iterates for each time pushbutton is pressed.
mypoint = drawpoint(handles.axes1);
handles.el = addlistener(mypoint, 'ROIMoved', @mypointmoved_Callback);
因此,如果我的 pushbutton1
创建另一个点;我怎样才能得到它,所以每个点都有多个 addlistener
?
我需要多个 handles.el
吗?
如果有兴趣,我的
继续我之前的回答...
- 为每个新点添加一个监听器。
- 你不需要有多个
handles.el
,你可以忽略el
,因为你根本不用它。 - 您可以存储创建点的句柄数组(如果需要)。
- 您可以将点的索引存储在
mypoint.UserData
中(如果需要的话)。 - 在监听器的回调中,可以从数组中获取任意一个点,也可以查看触发事件的点的索引。
这里是代码的相关部分(请阅读评论):
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
axes(handles.axes1);
mypoint = drawpoint;
%Add event listenr to every point
%if ~isfield(handles, 'el')
addlistener(mypoint, 'ROIMoved', @mypointmoved_Callback);
%end
%Save an array of handles to created points:
if ~isfield(handles, 'mypoints')
mypoint.Color = [0.7410, 0.2, 0]; %First point has a different color.
handles.mypoints = mypoint; %Add first point.
%Distance from the first added point.
handles.distx = 0;
handles.disty = 0;
set(handles.edit1, 'String', '0'); %Display the distance from the first added point.
else
handles.mypoints(end+1) = mypoint; %Add a new point to the end of the array
end
points_counter = length(handles.mypoints); %Number of points - use as "Point serial number".
mypoint.UserData = points_counter; %Store the index of the point in UserData property
% Update handles structure
guidata(hObject, handles);
function mypointmoved_Callback(src, eventData)
handles = guidata(src.Parent); %Get the handles using the parent of the point (the axes).
if src.UserData == 1
%Example for using the index of the point stored in UserData property.
set(handles.edit1, 'String', '0');
return;
end
px = src.Position(1);
py = src.Position(2);
%Get the first point that added by the user
p0 = handles.mypoints(1);
%Get the x,y position of the first point that added by the user
p0x = p0.Position(1);
p0y = p0.Position(2);
%Compute the distance from p0:
distx = abs(px - p0x);
disty = abs(py - p0y);
handles.distx = distx;
handles.disty = disty;
dist = norm([distx, disty]); %Euclidean distance
%Display the euclidean distance from p0.
set(handles.edit1, 'String', num2str(dist));
drawnow
该示例显示用户移动的点到第一个(用户)添加的点的欧式距离。