フィルターのクリア

How do you implement 'AddingVertex' event callback

3 ビュー (過去 30 日間)
Joseph Cammarata
Joseph Cammarata 2024 年 1 月 9 日
回答済み: Aiswarya 2024 年 1 月 25 日
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

採用された回答

Aiswarya
Aiswarya 2024 年 1 月 25 日
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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by