Drawing ROI in app designer produces error after using drawrectangle more than one time

7 次查看(过去 30 天)
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 个评论
Image Analyst
Image Analyst 2023-9-16
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.
Vitek Stepien
Vitek Stepien 2023-9-18
I'm attaching an mlappinstall file, it basically lets you "install" the app into your MATLAB to run as other MATLAB apps like Curve Fitter or Optimization. The only reason I'm doing that is that I would have to add about half a dozen other function files to it, and with the mlappinstall they are all included. I'm also attaching a sample data file, so you can run it.
To recreate my error, follow this procedure:
  • run the app and let it load a couple seconds (the three axes on the left should have some plots, the right ones will remain blank)
  • go to "Spectral decomposition" tab
  • hit "Decompose" button in the bottom left, after it loads, the middle axis should have a coolorful yellow-blue image
  • select the button with the square from the menu bar on top and draw a rectangular ROI on the middle axis
  • now, if you select that button again the ROI will correctly disappear and you can draw new one. However, if you instead select any tool in the middle axis, e.g. Zoom or pan, and try to draw another ROI, a bunch of warnings appear and the app starts to behave ill.
  • If I select a Zoom tool, zoom in on a region, and deselect the Zoom tool, I can draw another ROI without getting the warnings, but the previous rectangle doesn't disappear, and I want to only have one at a time.
Let me know if you run into trouble. The app requires wavelet toolbox for sure, I don't remember if there are any other ones necessary. Thanks!

请先登录,再进行评论。

回答(1 个)

Eric Delgado
Eric Delgado 2023-12-7
编辑:Eric Delgado 2023-12-8
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

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by