【问题标题】:MATLAB GUI: Update textbox when moving drawpointMATLAB GUI:移动绘图点时更新文本框
【发布时间】:2019-10-16 05:51:43
【问题描述】:

我有一个 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);

【问题讨论】:

    标签: matlab matlab-guide


    【解决方案1】:

    您可以在创建点后添加事件侦听器。

    1. 创建点:

      mypoint = drawpoint;
      
    2. 仅在不存在时添加事件侦听器(将其添加到handles):

      %Add event listenr only if not exist
      if ~isfield(handles, 'el')
          handles.el = addlistener(mypoint, 'ROIMoved', @mypointmoved_Callback);
      end
      
    3. 在回调函数中更新编辑框:

      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
      

    这里是简化版的代码(我把你的一些代码放在了cmets中):

    % --- 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
    

    【讨论】:

    • 啊,不知道它可以这样工作。我今天会试一试!
    • 所以,试一试(如果我有什么误解,很抱歉),我无法访问函数mypointmoved_Callback 中的handles 变量。因此我无法执行set(handles.edit1, 'String', num2str(handles.distx)); 行。如何将所有句柄等传递到mypointmoved_Callback
    • 它似乎没有通过所有的句柄变量(即图中的文本框,但不在轴内(如果有意义的话)。我在玩东西,但仍然没用。
    • 问题出在mypoint作为src。它不包含来自pushbutton 所在窗口的GUI 变量。只需要一种从两个 GUI 窗口传递变量的方法。
    • 你可以随时使用setappdata(groot, 'my_handles, handles)handles = getappdata(groot, 'my_handles)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 2014-03-15
    相关资源
    最近更新 更多