Generally, you should assign a callback to your imrect . For instance:
x = imrect(); x.addNewPositionCallback( @(x)(disp('The rect has changed')))
The callback must receive additional parameters, such as the image and other axes, using anonymous functions.
I wrote a small piece of code that does what you want. You should add border checks as I was not worried. It updates CData instead of launching imshow when moving the rectangle, so it is pretty smooth.

function Zoomer figure(); highResImage = imread('peppers.png'); lowResImage = imresize(highResImage,0.5); a1 = subplot(2,1,1); a2 = subplot(2,1,2); imshow(lowResImage,'Parent',a1); initialPosition = [10 10 100 100]; lowResRect = imrect(a1,initialPosition); lowResRect.addNewPositionCallback( @(pos)Callback(pos,a2,highResImage)); Callback( initialPosition , a2, highResImage); end function Callback(position,axesHandle, highResImage) position = position * 2; x1 = position(1); y1 = position(2); x2 = position(1) + position(3); y2 = position(2) + position(4); highResThumbnail = highResImage( round(y1:y2),round(x1:x2),:); if isempty( get(axesHandle,'Children')) imshow(highResThumbnail,'Parent',axesHandle); else imHandle = get(axesHandle,'Children'); oldSize = size(get(imHandle,'CData')); if ~isequal(oldSize, size(highResThumbnail)) imshow(highResThumbnail,'Parent',axesHandle); else set( imHandle,'CData', highResThumbnail); end end end
source share