How could I make a callback that updates when the user moves an roi?

11 views (last 30 days)
I'm making an app which loads and displays the same image side by side in app.UIAxes and app.UIAxes2. I have a rectangle roi object in app.UIAxes2 which the user can move around and adjust the size of. I want to take the coordiantes of the roi in app.UIAxes2 and use that to interactively zoom to those coordinates for the other UIAxes by changing the XLim and YLim accordingly. My issue is I don't know how to setup the callback so that it responds to a change in the position.
My question is really, is it possible to setup a value changing funtion that is called everytime the position values change in the roi, and if so how would I go about it? This would be similar to a value changing callback for a slider but the difference/issue being that draw rectangle is not part of the component library for app designer.
Any help/guidance would be appreciated.
  1 Comment
Arthur Roué
Arthur Roué on 11 Mar 2020
I'm not familiar with ROI rectangle, but you could try to add a listener on it's DrawingArea property.
addlistener(roi, 'DrawingArea', 'PostSet', @foo)

Sign in to comment.

Accepted Answer

Turlough Hughes
Turlough Hughes on 6 Jan 2022
This is what I had in mind:
function [] = interactiveZoomDemo()
I = imread('saturn.png');
hf = figure();
% Reference
roiPosition = [120 180 400 450];
roi = drawrectangle('Position', roiPosition);
% Close up - note that LimitsChanged also works on uiaxes.
ax = subplot(1,2,2);
addlistener(ax.XAxis, 'LimitsChanged', @updateROI); % extra feature
addlistener(ax.YAxis, 'LimitsChanged', @updateROI);
% Set initial axis limits
ax.XLim = [roiPosition(1) roiPosition(1)+roiPosition(3)];
ax.YLim = [roiPosition(2) roiPosition(2)+roiPosition(4)];
% Set figure width/height
hf.Position(3:4) = [850 400];
function updateZoomLimits(~, evt)
% Updates axes limits on RHS to match the ROI's position
p = evt.CurrentPosition;
ax.XLim = [p(1) p(1)+p(3)];
ax.YLim = [p(2) p(2)+p(4)];
function updateROI(~,~)
% Updates the ROI's position when panning or zooming in/out of the
% image on the right.
p = [ax.XLim; ax.YLim];
roi.Position = [p(1) p(2) p(3)-p(1) p(4)-p(2)];

More Answers (0)




Community Treasure Hunt

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

Start Hunting!

Translated by