MATLAB GUI:移动绘图点时更新文本框

MATLAB GUI:移动绘图点时更新文本框,matlab,matlab-guide,Matlab,Matlab Guide,我有一个GUI,用户在其中单击按钮以放置点(drawpoint)。放置点后,将计算点与之前选择的静态点之间的欧氏距离 我希望能够移动按钮创建的点;这样,移动点后,将重新计算欧几里德距离并将其放入文本框中 我尝试使用addlistener(在GUI\u OpeningFcn位置)创建点;但是,我不知道如何执行此操作,因为在创建按钮之前,句柄不存在 因此,问题是:如何动态执行计算并在移动点时吐出值?下面是按钮的代码(我想要它)。但移动点后如何重新计算 也许,可以使用WindowbuttonDownF

我有一个GUI,用户在其中单击按钮以放置点(
drawpoint
)。放置点后,将计算点与之前选择的静态点之间的欧氏距离

我希望能够移动按钮创建的点;这样,移动点后,将重新计算欧几里德距离并将其放入文本框中

我尝试使用
addlistener
(在GUI\u 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;
    
  • 仅在不存在时添加事件侦听器(将其添加到
    句柄中)

  • 更新回调函数中的编辑框:

    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
    
  • 下面是代码的简化版本(我将您的一些代码放在注释中):


    啊,不知道会这样。我今天要试一试!因此,尝试一下(如果我误解了什么,很抱歉),我无法访问函数
    mypointmoved\u回调
    中的
    句柄
    变量。因此,我无法执行
    集(handles.edit1,'String',num2str(handles.distx))行。如何将所有句柄等传递到
    mypointmoved\u回调
    ?它似乎不会传递所有句柄变量(即图形中的文本框,但不会传递到轴中(如果有意义的话)。我正在玩东西,但仍然没有用。问题在于作为src的
    mypoint
    。它不包含
    按钮所在窗口中的GUI变量。只需要一种方法从两个GUI窗口传递变量。您可以始终使用
    setappdata(groot,'my_handles,handles)
    handles=getappdata(groot,'my\u 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)); %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