问题
I have a GUI where the user clicks a button to place a point (drawpoint
). After placing the point, the Euclidean distance is calculated between it and a static point chosen prior.
I want to be able to move point created by the pushbutton; such that, after moving the point the Euclidean distance is recalculated and spit into a textbox.
I tried using addlistener
(in the GUI_OpeningFcn postion) for the created point; however, I cannot figure out how to do this, as the handle doesn't exist until after the pushbutton is created.
Thus the issue: How can I dynamically perform a calculation and spit out the value upon moving a point? Below is the code for the pushbutton (which does what I want). But how can I recalculate after moving the point?
Perhaps, could this be down using WindowbuttonDownFcn
? Again, just not sure how to incorporate this into the 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);
回答1:
You can add the event listener after creating the point.
Create the point:
mypoint = drawpoint;
Add event listener only if not exist (add it to
handles
):%Add event listenr only if not exist if ~isfield(handles, 'el') handles.el = addlistener(mypoint, 'ROIMoved', @mypointmoved_Callback); end
Update the edit box in the callback function:
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
Here is a simplified version of the code (I put some of your code in comments):
% --- 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
来源:https://stackoverflow.com/questions/58402447/matlab-gui-update-textbox-when-moving-drawpoint