Creating an event for "DrawRectangle" such as right click on mouse
Show older comments
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
dpb
on 17 Nov 2025
Jason
on 17 Nov 2025
dpb
on 17 Nov 2025
The right-click is automagically assigned to the datatip display callback as a generic property.
Jason
on 17 Nov 2025
Jason
on 17 Nov 2025
"As the right click appears to not be available..."
You misunderstood what I said -- the right click event is assigned to the datatips by default, but you can override it...
I dunno if this will work interactively here or not...
hR=rectangle();
xlim([-5 5]),ylim(xlim)
hR.ButtonDownFcn=@(src,event)disp(event);
I can't trigger an event here, but locally, I get
>> hR.ButtonDownFcn=@(src,event)disp(event); % just show the event data
Hit with properties:
Button: 1
IntersectionPoint: [0.0115 0 0]
Source: [1×1 Rectangle]
EventName: 'Hit'
Hit with properties:
Button: 3
IntersectionPoint: [0.0115 1 0]
Source: [1×1 Rectangle]
EventName: 'Hit'
>>
You can distinguish left and right by the Button property of the Event.
BTW, I use a Logitech trackball rather than a traditional two-button mouse so the left and right are 1 and 3, the middle scroll wheel is 2....
Jason
on 18 Nov 2025
Double Hmmmm....I don't have the toolbox so not familiar with drawrectangle. The doc shows that unfortunately, for some reason the Rectangle object it (drawrectangle) returns doesn't have any callbacks; consequently what your code does is to add the 'ButtonDownFcn' field to the object handle variable, but it is creating a new field for the local variable; it can't create the event so there's nothing going to happen to trigger it. That seems very peculiar and an unexpected lack of a feature but you would have to ask Mathworks support for the rationale behind that design.
It appears the only way with the ROI stuff is to use addlistener to trigger on an event. You'll have to use the events function on the handle to see what events are supported that way. W/O the toolbox not much more I can tell you, sorry...
OK, I did finally come across <Events> in an example about adding listener to ROI. There's another section about if in AppDesigner app I didn't explore that may be pertinent as well.
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.
Answers (0)
Categories
Find more on ROI-Based Processing 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!
