MATLAB Answers

Matt J

How to zoom in/out on an axis programmatically

Asked by Matt J
on 22 May 2013
Latest activity Commented on by Briana Sexton-Stallone on 17 Jun 2016

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

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.


No products are associated with this question.

1 Answer

Answer by Youssef Khmou
on 22 May 2013
 Accepted answer


There is a function "zoom" with the following options :

 zoom on 
 zoom off
 zoom out
 zoom reset 
 zoom xon
 zoom yon
 zoom(fig, option) 
 h = zoom(figure_handle)


Matt J
on 2 Jun 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?

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);
		zoomFactor = get(hObject,'Value');
		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);
	return; % from sldZoom_Callback

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!