How to zoom in/out on an axis programmatically

93 views (last 30 days)
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 Comment
John
John on 17 Apr 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.

Sign in to comment.

Accepted Answer

Youssef  Khmou
Youssef Khmou on 22 May 2013
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 Comments
Kyle Chappell
Kyle Chappell on 11 Jan 2017
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.

Sign in to comment.

More Answers (2)

Mike Smith
Mike Smith on 17 Aug 2016
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 Comment
Mohamed Abdelhamid
Mohamed Abdelhamid on 17 May 2021
Edited: Mohamed Abdelhamid on 17 May 2021
ax = gca;
ax.XLim = [lowerxvalue upperxvalue];
ax.YLim = [loweryvalue upperyvalue];

Sign in to comment.


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

Categories

Find more on Data Exploration in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!