MATLAB GUI:移动绘图点时更新文本框 [英] MATLAB GUI: Update textbox when moving drawpoint

查看:319
本文介绍了MATLAB GUI:移动绘图点时更新文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GUI,用户可以在其中单击按钮放置一个点(drawpoint).放置该点后,将计算出它与先前选择的静态点之间的欧几里得距离.

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.

我尝试对创建的点使用addlistener(在GUI_OpeningFcn位置);但是,我无法弄清楚该怎么做,因为直到创建按钮后该手柄才存在.

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?

也许,使用WindowbuttonDownFcn可以解决此问题吗?同样,只是不确定如何将其合并到GUI中.

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);

推荐答案

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

You can add the event listener after creating the point.

  1. 创建点:

  1. Create the point:

mypoint = drawpoint;

  • 仅在不存在事件侦听器时添加事件侦听器(将其添加到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
    

    这篇关于MATLAB GUI:移动绘图点时更新文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆