How to restrict zooming out of an image displayed

Illustration
Manish_ashud - 2022-11-02T14:06:00+00:00
Question: How to restrict zooming out of an image displayed

How to restrict zooming out of an image displayed in a UIAxes component beyond the original axes limits In a figure-based axes object, if you zoom out of an image displayed using "imshow", the zoom-out capability is restricted to axis limits corresponding to the size of the image. However, as of MATLAB R2020b for UIAxes, this is not the case and it allows zooming out beyond the original size of the image, thereby overshrinking the image.   Is it possible to restrict this behavior and not allow the image to overshrink when zooming out?

Expert Answer

Profile picture of Kshitij Singh Kshitij Singh answered . 2025-11-20

One possible workaround to this would be to use the "ActionPostCallback" callback function for the zoom object corresponding to the UIAxes. Please take a look at the following example which illustrates the workaround. Here, every time the user tries to zoom out beyond the original axes limits, the limits are reset to the original value:

 

a=uiaxes;
imshow(imread('cameraman.tif'),'Parent',a);
origXLim=a.XLim;
origYLim=a.YLim;
% Listen to zoom events
h = zoom(a);
set(h,'ActionPostCallback',@(x,y)mypostcallback(x,y,origXLim,origYLim));
set(h,'Enable','on');

function mypostcallback(obj,evd,origXLim,origYLim)
    newLim = get(evd.Axes,'XLim');
        if (newLim(2)-newLim(1)) > (origXLim(2)-origXLim(1))
            evd.Axes.XLim=origXLim;
            evd.Axes.YLim=origYLim;
        end
end

 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!