您可以在创建点之后添加事件侦听器。
创建点:mypoint = drawpoint;
仅在不存在时添加事件侦听器(将其添加到handles ):%Add event listenr only if not exist if ~isfield(handles, 'el') handles.el = addlistener(mypoint, 'ROIMoved', @mypointmoved_Callback); end
更新回调函数中的编辑框:function mypointmoved_Callback(src, eventData) handles = guidata(src.Parent); %Get the handles using the parent of the point (the axes). handles.distx = src.Position(1); handles.disty = src.Position(2); set(handles.edit1, 'String', num2str(handles.distx)); %Simplified version of your code. drawnow
这是代码的简化版本(我在注释中放了一些代码):
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
%h = findobj('Name', 'N');
%Ndata = guidata(h);
%axes(Ndata.axes1);
axes(handles.axes1);
mypoint = drawpoint;
%Add event listenr only if not exist
if ~isfield(handles, 'el')
handles.el = addlistener(mypoint, 'ROIMoved', @mypointmoved_Callback);
end
handles.distx = mypoint.Position(1);
handles.disty = mypoint.Position(2);
% xp = Ndata.xpix;
% yp = Ndata.ypix;
% handles.poix = abs(double(handles.distx) - double(Ndata.ISOx))/str2double(xp.String);
% handles.poiy = abs(double(handles.disty) - double(Ndata.ISOy))/str2double(yp.String);
% handles.poi = sqrt(handles.poix^2 + handles.poiy^2)+1.3;
% set(handles.edit1, 'Value', handles.poi);
%set(handles.edit1, 'String', num2str(handles.poi));
set(handles.edit1, 'String', num2str(handles.distx));
% 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).
handles.distx = src.Position(1);
handles.disty = src.Position(2);
set(handles.edit1, 'String', num2str(handles.distx));
drawnow