In the GUI window, when i click mouse to draw something  a unnecessary line is being drawn. Irrespective of how many times I tried I'm unable to get rid of that. This causing me to get an irregular shape of my desired object. If possible include a eraser to erase such things and also check what went wrong in my code. function gestureRecognitionGUI()
    % Create the interface figure
    fig = figure('Name', 'Gesture Recognition For Vacuum Robot', 'Visible', 'off', ...
                 'Position', [500 500 600 600], 'NumberTitle', 'off');
    
    % Draw axes for gestures
    gestureAxes = axes('Parent', fig, 'Units', 'Normalized', 'Position', [.2 .2 .6 .6]);
    set(gestureAxes, 'ButtonDownFcn', @startDraw);
    set(get(gestureAxes, 'Children'), 'ButtonDownFcn', @startDraw)
    
    % Create a popup menu for choosing gesture classes
    classNames = {'Charge Battery', 'Area Selection', 'Clean Mop', ...
                  'Pause Operation', 'Start Operation', 'Vacuum Surface', 'Mop Surface'};
    gesturePopUp = uicontrol('Style', 'popupmenu', 'String', classNames, 'Units', 'Normalized', ...
                             'Position', [.15 .05 .7 .1]);
    
    % Create a button for clearing the axes
    clearButton = uicontrol('Style', 'pushbutton', 'String', 'Clear', 'Units', 'Normalized', ...
                            'Position', [.45 .85 .1 .05], 'Callback', @clearGestureAxes);
    
    % Set the figure to visible
    set(fig, 'Visible', 'on');
    
    function startDraw(src, ~)
        % This function responds to the mouse button press on the axes
        startP = get(src, 'CurrentPoint');
        set(src, 'UserData', startP(1,1:2))
        set(fig, 'WindowButtonMotionFcn', @drawing)
        set(fig, 'WindowButtonUpFcn', @stopDraw)
    end
    
    function drawing(~, ~)
        % This function follows mouse movement and draws the gesture
        if isequal(get(fig, 'CurrentObject'), gestureAxes)
            p1 = get(gestureAxes, 'UserData');
            p2 = get(gestureAxes, 'CurrentPoint');
            p2 = p2(1, 1:2);
            line([p1(1), p2(1)], [p1(2), p2(2)], 'Parent', gestureAxes, 'linewidth', 2, 'Color', 'red');
            set(gestureAxes, 'UserData', p2)
        end
    end
    
    function stopDraw(~, ~)
        % This function responds to the mouse button release on the axes
        set(fig, 'WindowButtonMotionFcn', '');
        
        % Save the gesture points as a mat file
        classname = classNames{get(gesturePopUp, 'Value')};
        gestureLines = findobj(gestureAxes, 'Type', 'Line');
        gesturePoints = [];
        for i = 1:length(gestureLines)
            gesturePoints = [gesturePoints; [gestureLines(i).XData' gestureLines(i).YData']];
        end
        
        % Check if the mat-file exists, if not, create it
         if ~(exist(fullfile('gestures/', sprintf('%s.mat', classname)), 'file') == 2)
            gestureData = {};
            save(fullfile('gestures/', sprintf('%s.mat', classname)), 'gestureData');
         end
        
        % Load and update the existing mat-file
        load(fullfile('gestures/', sprintf('%s.mat', classname)));
        if isempty(gestureData) || isempty(gestureData{end})
            gestureData{end + 1} = gesturePoints;
        else
            gestureData{end + 1} = cell(size(gestureData{1}));
            gestureData{end}{1} = gesturePoints;
        end
        save(fullfile('gestures/', sprintf('%s.mat', classname)), 'gestureData');
    end
    
    function clearGestureAxes(~, ~)
        % Clear the gesture drawing axes
        gestureLines = findobj(gestureAxes, 'Type', 'Line');
        delete(gestureLines);
    end
end