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

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

Review <Capturing Mouse Clicks> for an overview of how MATLAB responds.

Hi, im already use hittest to print out the x,y pos in an image.

I notice also this doesnt memtion right mouse clicks

The right-click is automagically assigned to the datatip display callback as a generic property.

Oh o.k, meaning i cant use it tHen?

As the right click appears to not be available, Could I ask how i handle the Moved event but make it trigger only once both rectangles have been moved?

"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....
Hmm, so I have this in a button to test: But I get nothing.
ax=app.UIAxes;
numROIs = 2;
roiPos = zeros(numROIs,4);
for cnt = 1:numROIs
hrect = drawrectangle(ax);
roiPos(cnt,:) = hrect.Position
cnt
end
hrect.ButtonDownFcn=@(src,event)disp(event) % hrect.ButtonDownFcn=@(src,event)disp(event)
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.
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?
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

Asked:

on 17 Nov 2025

Edited:

dpb
on 18 Nov 2025

Community Treasure Hunt

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

Start Hunting!