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

How to zoom in/out on an axis programmatically

Asked by Matt J on 22 May 2013

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.


Matt J


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)


Image Analyst on 2 Jun 2013

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 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?

Image Analyst on 2 Jun 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);
		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
Youssef  KHMOU

Contact us