Drawing ROI in app designer produces error after using drawrectangle more than one time
    11 views (last 30 days)
  
       Show older comments
    
Hi,
I'm interactively drawing an ROI with drawrectangle in app designer.  It works great, I don't have problems with it being too slow.  However, when I delete the first ROI using its context menu and try to do it again, it throws the following warning (it works great if I delete it by pressing my DrawRectROI button again):
    Warning: Setting the "KeyPressFcn" property is not permitted while this mode is active. 
    > In matlab.graphics.interaction.internal/WebFigureModeInteractionData/localModeWarn
    In matlab.graphics.interaction.internal.WebFigureModeInteractionData
    In images.roi.internal/ROI/setEmptyCallbackHandle
    In images.roi.internal/ROI/draw
    In drawrectangle (line 217)
    In Coherence_analyzer/DrawRectROIClicked (line 1036)
    In matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 62)
    In matlab.ui.internal.controller/WebToolMixinController/fireActionEvent (line 218)
    In matlab.ui.internal.controller/WebToolMixinController/handleEvent (line 205)
    In matlab.ui.internal.controller.WebToolMixinController>@(varargin)obj.handleEvent(varargin{:}) (line 186)
    In uiwait (line 81)
Moreover, ever time after that, when I try to do it again, this warning shows up N times at the same time in the command window, where N is the number of times I tried to do it after the first time.  MATLAB remains "Busy" and unresponsive until I Ctrl-C kill it, and then it show the following error:
    Operation terminated by user during uiwait
    In images.roi.internal.ROI/draw
    In drawrectangle (line 217)
        h.draw;
    In Coherence_analyzer/DrawRectROIClicked (line 1036)
                app.roi = drawrectangle(app.UIAxes15, "FaceSelectable", true, "Color", "black");
    In matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 62)
                newCallback = @(source, event)executeCallback(ams, ...
    Error using matlab.ui.internal.controller.WebToolMixinController/fireActionEvent
    Interrupt while evaluating PushTool ClickedCallback.
    Operation terminated by user during linkaxes
    In Coherence_analyzer/DrawRectROIClicked (line 1040)
                linkaxes([app.UIAxes14, app.UIAxes15],'off')
    In matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 62)
                newCallback = @(source, event)executeCallback(ams, ...
    Error using matlab.ui.internal.controller.WebToolMixinController/fireActionEvent
    Interrupt while evaluating PushTool ClickedCallback.
    Index exceeds the number of array elements. Index must not exceed 0.
Based on my previous troubleshooting and playing with it, I suspect that it has to do with leftover listeners that somehow prevent me from setting them up again.  I can't show the whole code, but below are (IMO) relevant functions and pieces of code:
% Callback function: DrawRectROI
        function DrawRectROIClicked(app, event)
            persistent listenerhandle
            if ~isempty(app.roi) % This "if" fixed some initial problems with repeated use of this functionality
                delete(listenerhandle)
                delete(app.roi)
            end
            app.roi = drawrectangle(app.UIAxes15, "FaceSelectable", true, "Color", "black");
            listenerhandle = addlistener(app.roi, 'ROIMoved', @(varargin)roimoved(app));
            % I suspect this listener might be the culprit, but I'm not sure how to make it delete itself.
            addlistener(app.roi, 'DeletingROI', @(varargin)roidelete(app, listenerhandle)); 
            app.roi.UserData = app.roi.Position; % Storage for old Position
            linkaxes([app.UIAxes14, app.UIAxes15],'off')
            roimoved(app)
        end
        function roimoved(app)
            timelims = app.roi.Position(1) + [0 app.roi.Position(3)];
            xlim(app.UIAxes14, timelims)
            app.roi.UserData = app.roi.Position; % Storage for old Position
        end
        function roidelete(~, listener_handle)
            delete(listener_handle)
        end
I read a couple other posts on the forum, like this:
but it doesn't address the same issue.  Any help will be greatly appreciated!
4 Comments
  Image Analyst
      
      
 on 16 Sep 2023
				I don't know what an install file is.  If it's not one of the allowed extension types you'll have to put them into a zip archive.
Answers (1)
  Eric Delgado
      
 on 7 Dec 2023
        
      Edited: Eric Delgado
      
 on 8 Dec 2023
  
      I had the same warning message "Warning: Setting the "KeyPressFcn" property is not permitted while this mode is active.". It's a strange behaviour.
So... now I am creating another listener that kills all listeners (including itself) when the ROI is deleted. Since then, I am not receiving warnings anymore Please, let me know if it works for you! 
% startup of my app
warning('off', 'MATLAB:structOnObject')
% creation of ROI in app.UIAxes1
disableDefaultInteractivity(app.UIAxes1)
hROI = drawrectangle(app.UIAxes1, Deletable=0, FaceSelectable=0);
enableDefaultInteractivity(app.UIAxes1)
if isempty(hROI.Position)
    delete(hROI)
    return
end
addlistener(hROI, 'MovingROI', @app.filter_ROICallbacks);
addlistener(hROI, 'ROIMoved',  @app.filter_ROICallbacks);
addlistener(hROI, 'ObjectBeingDestroyed', @(src, ~)cleanupFcn(src));
% cleanupFcn
function cleanupFcn(hROI)
    try
        listenersList = struct(hROI).AutoListeners__;
        cellfun(@(x) delete(x), listenersList)
    catch
    end
end
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!