Creating an event for "DrawRectangle" such as right click on mouse

69 views (last 30 days)
Jason
Jason on 17 Nov 2025 at 15:41
Edited: dpb on 18 Nov 2025 at 21:49
Hello, i am drawing 2 rectangles on an image
ax=app.UIAxes; % Axes that image is on
numROIs = 2;
roiPos = zeros(numROIs,4);
for cnt = 1:numROIs
hrect = drawrectangle(ax);
roiPos(cnt,:) = hrect.Position;
cnt
end
I want to be able to perform a calculation (e.g standard deviation) of each region by a right click or something else rather than a ROImoving or ROImoved event. (the reason is I dont want the calcs firing off whilst moving or e.g one rectangle is in position (stopped moving) but the 2nd rectangle still needs to be positioned)
Any pointers, I see the below but I can't find a right mouse click event or some other suggestion
addlistener(roi,'MovingROI',@allevents);
addlistener(roi,'ROIMoved',@allevents);
function allevents(src,evt)
evname = evt.EventName;
switch(evname)
case{'MovingROI'}
disp(['ROI moving previous position: ' mat2str(evt.PreviousPosition)]);
disp(['ROI moving current position: ' mat2str(evt.CurrentPosition)]);
case{'ROIMoved'}
disp(['ROI moved previous position: ' mat2str(evt.PreviousPosition)]);
disp(['ROI moved current position: ' mat2str(evt.CurrentPosition)]);
end
end
  10 Comments
Jason
Jason on 18 Nov 2025 at 17:53
Edited: Jason on 18 Nov 2025 at 17:56
Thanks, So I have managed to create the ROIclicked event:
function results = RectangleEvents(app,src,evt)
evname = evt.EventName
switch(evname)
case{'MovingROI'}
disp(['ROI moving previous position: ' mat2str(evt.PreviousPosition)]);
disp(['ROI moving current position: ' mat2str(evt.CurrentPosition)]);
case{'ROIMoved'}
disp(['ROI moved previous position: ' mat2str(evt.PreviousPosition)]);
disp(['ROI moved current position: ' mat2str(evt.CurrentPosition)]);
case{'ROIClicked'}
disp('ROI Clicked')
end
end
And I activate like this:
ax=app.UIAxes;
numROIs = 1;
roiPos = zeros(numROIs,4);
for cnt = 1:numROIs
hrect = drawrectangle(ax);
roiPos(cnt,:) = hrect.Position
cnt
end
addlistener(hrect,'ROIClicked',@app.RectangleEvents);
But know even when I move the rectangle, it fires the ROIClicked event - I guess because you need to click on it to move it.
Looking at this class: images.roi.ROIClickedEventData Class
I see it has a "SelectionType" Properties, so I can use event.SelectionType which reports back e.g.'left.
However, I still want to retain the ROIMoved event, how can I stop the ROIClicked event from over ruling this?
dpb
dpb on 18 Nov 2025 at 19:06
Edited: dpb on 18 Nov 2025 at 21:49
It appears you would have to look at the area on which the click occurs as well and take action if click the face area instead of edge. Does appear to be very complicated to do anything with, agreed.
Keep digging at the doc, there's also an example with using a wait that might be of some interest in being able to control what happens when after the initial selection click ... in it, they unassign the initial callback function before resuming code -- maybe that's the key.
I really don't know and don't have any way to try it out not having the toolbox.
The other thread about reaction time may have something of interest, too....I wasn't aware of it, but waitforbuttonpress programmaticallly shifts the focus to the figure without a manual keystroke/mouse click...maybe that would be the way to be able to avoid the selection ROI click you're now getting that isn't of interest.
Or, another thought -- maybe the callback should first be on the 'ROISelected' property and only then set the ROI Click callback inside it once the ROI is selected...I dunno, this is far more involved than I've ever tried to build a GUI.

Sign in to comment.

Answers (0)

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!