Clear Filters
Clear Filters

How do you implement 'AddingVertex' event callback

4 views (last 30 days)
I have been using app designer to open an image and then use the drawpolyline function to create an roi. However, I want to add a callback function to prevent the user from trying to add a vertex outside of the drawing area (even though I specify drawing area, a click outside of the drawing area results in a vertex on the perimeter).
I tried adding the code as shown below but it never goes into the callback function. I've tried with other events and the only ones I can get to work are from the 'images.roi.ROIMovingEventData' event class. What am I missing?
% draw: the main drawing method to allow for selecting and drawing
% annotations
function draw(app)
% Start drawing the polyline
try
% Draw a polyline
roi = drawpolyline("Parent", app.UIAxes);
app.roi_position = roi.Position;
catch
% The polyline is getting drawn in the LayerButton function
% and there is no need the data for this roi.
end
addlistener(roi,'VertexAdded',@(src,evt) recallPositionInUserData(src,evt));
function recallPositionInUserData(src,~)
src.Position = src.UserData;
end
end

Accepted Answer

Aiswarya
Aiswarya on 25 Jan 2024
Hi Joseph,
I understand that you want to add a callback function to prevent the user from adding a vertex outside of the drawing area (or 'roi'). The reason why your callback does not get triggered is because you are not linking it with the app. In order to link the callback to the app:
  1. The callback function must have 'app','src' and 'event' as the first three arguments.
  2. To assign the callback function to the 'addlistener', you have to specify the handle to the callback function as "@app.FunctionName".
You may refer to the following modified code to resolve your issue:
function draw(app)
% Start drawing the polyline
try
% Draw a polyline
% 1. Assign the polyline to app.roi
app.roi = drawpolyline("Parent", app.UIAxes);
app.roi_position = roi.Position;
catch
% The polyline is getting drawn in the LayerButton function
% and there is no need the data for this roi.
end
% 2. Modify the function handle
addlistener(app.roi,'VertexAdded',@(src,evt) @app.recallPositionInUserData(src,evt));
% 3. Add app as the first argument
function recallPositionInUserData(app,src,~)
src.Position = src.UserData;
end
end
You may refer to the following link for more information on creating callbacks in app designer: https://www.mathworks.com/help/matlab/creating_guis/write-callbacks-for-gui-in-app-designer.html

More Answers (0)

Community Treasure Hunt

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

Start Hunting!