How to zoom in/out on an axis programmatically

In a figure window toolbar, there are spyglass icons that let you zoom in and out on parts of an image or plot. I'd like to know how to do this programmatically.

1 commentaire

John
John le 17 Avr 2015
I have a figure with the plots and axes out of screen and no scroll bar. Please I need help shrinking the plot size.

Connectez-vous pour commenter.

 Réponse acceptée

hi,
There is a function "zoom" with the following options :
zoom on
zoom off
zoom out
zoom reset
zoom xon
zoom yon
zoom(factor)
zoom(fig, option)
h = zoom(figure_handle)

9 commentaires

Matt J
Matt J le 22 Mai 2013
Thanks. What I still can't see, though, is how you determine the "center" of the zoom. When zooming interactively, the position of the mouse pointer determines the position of the zoomed view, but I can't see how to set that with the ZOOM command.
hi Matt J, So far you can control the direction H/V in the figure :
% example
plot(randn(100,1));
h=zoom;
set(h,'Motion','horizontal','Enable','on');
zoom(2.3)
%set(h,'Motion','vertical','Enable','on');
Youssef  Khmou
Youssef Khmou le 23 Mai 2013
Modifié(e) : Youssef Khmou le 23 Mai 2013
hi Matt,
You have to control the figure properties then a zoom will be applied based on those parameters ( get(figure(1)), the property that concerns us is the "CurrentPoint" property, here is an example :
plot(randn(100,1))
Fig=figure(1);
position=get(Fig,'CurrentPoint');
new_position=[200 300]; % an example
set(Fig,'CurrentPoint',new_position);
h=zoom;
set(h,'Enable','on');
zoom(2)
The zoom is focused in that point , of course this approach may seem not efficient because you need preliminary knowledge about the data cursor .
Matt J
Matt J le 2 Juin 2013
Thanks, Youssef.
Yeah, it's always a pain because zoom(factor) is relative. So if you're zooming something like an image you always have to reset the zoom and then call it again with the factor, or else you have to keep track of the absolute zoom level yourself. I have code for a zoom scrollbar if you're interested.
Matt J
Matt J le 2 Juin 2013
Modifié(e) : Matt J le 2 Juin 2013
Well, I was mainly after a programmatic way to duplicate the spyglass figure window icon functionality... But how is the zoom scrollbar different from the spyglass?
Image Analyst
Image Analyst le 2 Juin 2013
Modifié(e) : Image Analyst le 2 Juin 2013
It's not. The scroll bar just give you an absolute zoom level, which is the scroll bar value. So then you just do zoom('out') everytime to reset it back to the starting zoom, and then zoom(scrollbarValue) in the callback of the scrollbar.
% --- Executes on slider movement.
function sldZoom_Callback(hObject, eventdata, handles)
% hObject handle to sldZoom (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% scrollbarValue = get(hObject,'Value');
% caption = sprintf('H value = %.2f', scrollbarValue);
% set(handles.txtZoom, 'string', caption);
try
zoomFactor = get(hObject,'Value');
axes(handles.axesImage);
zoom('out');
zoom(zoomFactor);
txtInfo = sprintf('Zoom Factor = %.2f (%d %%)\n\nOnce zoomed, you can pan by clicking and dragging in the image.', zoomFactor, round(zoomFactor * 100));
set(handles.txtInfo, 'String', txtInfo);
txtInfo = sprintf('Zoom Factor = %.2f\n\nOnce zoomed, you can pan by clicking and dragging in the image.', zoomFactor);
set(handles.sldZoom, 'TooltipString', txtInfo);
txtZoom = sprintf('Zoom Factor = %.2f (%d %%)', zoomFactor, round(zoomFactor * 100));
set(handles.txtZoom, 'String', txtZoom);
% if zoomFactor ~= 1
% else
% end
% Set up to allow panning of the image by clicking and dragging.
% Cursor will show up as a little hand when it is over the image.
set(handles.axesImage, 'ButtonDownFcn', 'disp(''This executes'')');
set(handles.axesImage, 'Tag', 'DoNotIgnore');
h = pan;
set(h, 'ButtonDownFilter', @myPanCallbackFunction);
set(h, 'Enable', 'on');
catch ME
message = sprintf('Error in sldZoom_Callback():\n%s', ME.message);
msgboxw(message);
end
return; % from sldZoom_Callback
what is hObject?
hObject is the object handle. It's passed as a default argument when accessing the callback function of uicontrol objects. For instance, if you have a uibutton, e.g. 'Go', 'Save', 'Next', the handle to the button object will be passed. This gives you access to the object properties like Position, String, buttonDownFcn, etc. and allows you to access their values or edit them.
This may not always be the case, I'm not an expert, but I've run into this a lot when building GUIs, whether using GUIDE or not.
To ensure the accuracy of what I've said on your own, I suggest you build something with GUIDE, set a breakpoint after a function calling hObject and using get(hObject) to view the accessible properties. Using get(handles.pushbutton1), for example, should be the same as get(hObject) when used in pushbutton1's callback function. I can't be sure, I don't have MATLAB open in front of me right now.
Feel free to correct me if I'm wrong. I'm always open to learning something new.

Connectez-vous pour commenter.

Plus de réponses (2)

Mike Smith
Mike Smith le 17 Août 2016

7 votes

If you know the zoom level that you want, you can do it by setting the Xlim and Ylim on the Children of the figure handle. So if you wanted to zoom the x axis between 400 and 500 you could do: h=gcf; set(h.Children,'Xlim',[400 500]);

1 commentaire

ax = gca;
ax.XLim = [lowerxvalue upperxvalue];
ax.YLim = [loweryvalue upperyvalue];

Connectez-vous pour commenter.

shaik
shaik le 12 Sep 2022
ax = gca;
ax.XLim = [lowerxvalue upperxvalue];
ax.YLim = [loweryvalue upperyvalue];

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by