ROI deleted event listener
Show older comments
Is there a listener event for when an ROI has been deleted from an axes? For example, like so:
addlistener(h,'ROIDeleted',@allevents);
But this doesn't seem to work.
If not, how could I check when a ROI shape has been deleted by the user through GUIDE?
Many thanks.
6 Comments
Németh Gergely
on 2 Nov 2020
I would be also very interested in that! I digged through the whole matlab documentation regarding roi-s but could not find it! I would appreciate some help!
Brian Derstine
on 9 Jan 2023
These callbacks should all be defined and listed in the Polyline Properties webpage, but for unexplained reasons they are not:
MovingROI
ROIMoved
ROIClicked
AddingVertex
VertexAdded
DeletingVertex
VertexDeleted
DeletingROI
(presumably there's another ROIDeleted or similar undocumented or private callback but I cannot determine what it is)
Brian Derstine
on 9 Jan 2023
Edited: Brian Derstine
on 9 Jan 2023
Thanks for the reply @Adam Danz That's a rather sneaky place to hide the events. Unfortunately, when a web user tries to search that page for any of those event names it turns up 0 results because that content is hidden until a user manually clicks on that "Events" div to uncollapse it. Very non-user-friendly. Also, that location on the page is not where Matlab's callbacks documentation states that callbacks can be found: "For a list of callbacks associated with a specific graphics object, see the properties of that object. For example, for a list of Figurecallbacks, see Figure Properties." (https://www.mathworks.com/help/matlab/creating_plots/create-callbacks-for-graphics-objects.html)
The Figure Properties page nicely lists a bunch of searchable callbacks under the normal Properties section under sub-headings like "Common Callbacks", "Keyboard Callbacks", etc. The images.roi... objects should all follow the same style.
I know what you mean @Brian Derstine about the collapsed sections. Whenever I text search a doc page, I expand all components first to make sure I don't miss something. This option is at the top, right of doc pages that contain expansion fields.

About the callbacks, note that these are not callback function. They are events.
Brian Derstine
on 10 Jan 2023
ah that's a helpful tip. thanks
Answers (2)
Matthew Siefring
on 10 Jan 2022
As an alternative to Adam's answer, you can use 'DeletingROI' as the event name parameter for the addListener function.
addlistener(h,'DeletingROI',@allEvents);
This way, the allEvents function will only be triggered when the user deletes the ROI interactively, instead of whenever a cleanup is triggered on the ROI object.
1 Comment
Adam Danz
on 13 Jan 2022
+1 good call
Use onCleanup to define a function that is evoked when the ROI is deleted. Store the onCleanup handle within the object.
Demo: Displays "ROI deleted" when the ROI object is deleted.
I = imread('baby.jpg');
figure
imshow(I)
h = images.roi.Circle(gca,'Center',[1000 1000],'Radius',500);
h.UserData.OnCleanup = onCleanup(@()allevents);
% Show figure, pause
drawnow;
pause(1)
% delete ROI obj
delete(h)
function allevents
fprintf('ROI deleted\n')
end
3 Comments
Németh Gergely
on 2 Nov 2020
Edited: Németh Gergely
on 2 Nov 2020
Wow, thank you very much for the fast answer. It works absolutely fine. Now I don't have to use a clear button in my app for removing the additional markers when I delete the ROI line object!
Adam Danz
on 2 Nov 2020
Glad I could help!
Brian Derstine
on 9 Jan 2023
Edited: Brian Derstine
on 9 Jan 2023
Is there an undocumented "ROIDeleted" function that gets called after "DeletingROI"? I am able to run code during the DeletingROI event but no matter what I do, the polyline always gets deleted after that function finishes. I want to prevent the polyline from being deleted all at once but allow individual vertices to be deleted. One way I tried to do this was by setting the "Deletable" property to 0/false, but after doing that I can no longer delete vertices on the polyline. I can add and move vertices no problem. Do I need to override the default context menu options and manually remove the Delete Polyline option somehow?
Categories
Find more on Build Interactive Tools in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!