matlabuser-interfacematlab-guide

Multiple GUI points with addlisteners


I have a GUI, where the user clicks a button to create a point. This point is used for measurements (i.e. calculating the distance between it and another static point).

    function pushbutton1_Callback(hObject, eventdata, handles)

    a = handles.calciter; % This iterates for each time pushbutton is pressed.

    mypoint = drawpoint(handles.axes1);

    handles.el = addlistener(mypoint, 'ROIMoved', @mypointmoved_Callback);

So if my pushbutton1 creates another point; how can I have it so there are multiple addlistener for each of the points?

Do I need to have multiple handles.el?

My previous question contains a bit more info if interested.


Solution

  • Continuation of my previous answer...

    Here is the relevant parts of the code (please read the comments):

    % --- Executes on button press in pushbutton1.
    function pushbutton1_Callback(hObject, eventdata, handles)
    axes(handles.axes1);
    
    mypoint = drawpoint;
    
    %Add event listenr to every point
    %if ~isfield(handles, 'el')
    addlistener(mypoint, 'ROIMoved', @mypointmoved_Callback);
    %end
    
    %Save an array of handles to created points:
    if ~isfield(handles, 'mypoints')
        mypoint.Color = [0.7410, 0.2, 0]; %First point has a different color.
        handles.mypoints = mypoint; %Add first point.
    
        %Distance from the first added point.
        handles.distx = 0;
        handles.disty = 0;
        set(handles.edit1, 'String', '0'); %Display the distance from the first added point.
    else    
        handles.mypoints(end+1) = mypoint; %Add a new point to the end of the array
    end
    
    points_counter = length(handles.mypoints); %Number of points - use as "Point serial number".
    mypoint.UserData = points_counter; %Store the index of the point in UserData property
    
    % 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).
    
    if src.UserData == 1
        %Example for using the index of the point stored in UserData property.
        set(handles.edit1, 'String', '0');
        return;
    end
    
    px = src.Position(1);
    py = src.Position(2);
    
    %Get the first point that added by the user 
    p0 = handles.mypoints(1);
    
    %Get the x,y position of the first point that added by the user
    p0x = p0.Position(1);
    p0y = p0.Position(2);
    
    %Compute the distance from p0:
    distx = abs(px - p0x);
    disty = abs(py - p0y);
    
    handles.distx = distx;
    handles.disty = disty;
    
    dist = norm([distx, disty]); %Euclidean distance
    
    %Display the euclidean distance from p0.
    set(handles.edit1, 'String', num2str(dist));
    drawnow
    

    The example displays the euclidean distance of the point that moved by the user to the first point added (by the user).