MATLAB GUI:移动绘制点时更新文本框
MATLAB GUI: Update textbox when moving drawpoint
我有一个 GUI,用户可以在其中单击一个按钮来放置一个点 (drawpoint
)。放置该点后,将计算它与之前选择的静态点之间的欧几里得距离。
我希望能够移动按钮创建的点;这样,在移动点后重新计算欧几里德距离并吐入文本框。
我尝试使用 addlistener
(在 GUI_OpeningFcn 位置)创建点;但是,我不知道该怎么做,因为在创建按钮之前句柄不存在。
因此问题是:如何动态执行计算并在移动点时吐出值?下面是按钮的代码(它做我想要的)。但是移动点后如何重新计算呢?
也许,这可以使用 WindowbuttonDownFcn
来解决吗?同样,只是不确定如何将其合并到 GUI 中。
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
h = findobj('Name', 'N');
Ndata = guidata(h);
axes(Ndata.axes1);
mypoint = drawpoint;
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));
% Update handles structure
guidata(hObject, handles);
您可以在创建点后添加事件监听器。
创建点:
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
我有一个 GUI,用户可以在其中单击一个按钮来放置一个点 (drawpoint
)。放置该点后,将计算它与之前选择的静态点之间的欧几里得距离。
我希望能够移动按钮创建的点;这样,在移动点后重新计算欧几里德距离并吐入文本框。
我尝试使用 addlistener
(在 GUI_OpeningFcn 位置)创建点;但是,我不知道该怎么做,因为在创建按钮之前句柄不存在。
因此问题是:如何动态执行计算并在移动点时吐出值?下面是按钮的代码(它做我想要的)。但是移动点后如何重新计算呢?
也许,这可以使用 WindowbuttonDownFcn
来解决吗?同样,只是不确定如何将其合并到 GUI 中。
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
h = findobj('Name', 'N');
Ndata = guidata(h);
axes(Ndata.axes1);
mypoint = drawpoint;
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));
% Update handles structure
guidata(hObject, handles);
您可以在创建点后添加事件监听器。
创建点:
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