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

9 次查看(过去 30 天)
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.

采纳的回答

Turlough Hughes
Turlough Hughes 2022-1-6
This is what I had in mind:
interactiveZoomDemo;
function [] = interactiveZoomDemo()
I = imread('saturn.png');
hf = figure();
subplot(1,2,1)
imshow(I)
% Reference
roiPosition = [120 180 400 450];
roi = drawrectangle('Position', roiPosition);
addlistener(roi,'MovingROI',@updateZoomLimits);
% 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);
imshow(I)
% 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)];
end
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)];
end
end

更多回答(0 个)

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by