File Exchange

image thumbnail


version (4.13 KB) by Rick Hindman
Turns the mouse into a pop-up magnifying glass to look at details of 2D plots.


Updated 11 May 2016

View License

Editor's Note: This file was a File Exchange Pick of the Week

Ever wish MATLAB had a magnifying glass so you could look at the details of a small region of your plots without having to zoom in and out again and again? Just run 'magnify.m' with the figure of interest as the current figure, then use the left mouse button to bring up a magnified veiw that you control. Or use the 'Ctrl' key while clicking to bring up a magnifying glass that 'locks' onto the figure when released (for use when copying and printing figures). Use the '<' and '>' keys to make the magnifying glass smaller or larger. Use '-' and '+' to decrease or increase the magnification level.

Cite As

Rick Hindman (2021). magnify (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (108)

Jiahang Li

xd h

where the magnify.m file should be saved?

Xingwang Yong

Sergio Yanez-Pagans


Aaron Freeman

Thanks man, that's exactly what I am looking for.
I will optimize my product based this file.

yuge sun

how to use

ruilin guo

Thank you !

guoqing zhao


炜彬 陈

how to exit?

Guangqi Ouyang


zeng chen

Thanks very much

J Tian

Thanks for the emarkable function, and could you please tell me how to exit this function?


Have added a simple modification to allow independent x/y magnification. Adjust the setting of the a2 userdata on line 31 to have three fields [2 2 0.2], adjust line 64 to call a2_param(3) not (2) for the position set, set line 67 to call a2_param(2) not (1), then add two extra elseifs copying lines 75-78 to modify a2_param(2) with the buttons of your choice (I went for [ and ]). Much more useful than just the single zoom.

biao li

very nice tool. It's very helpful. thank you !

Helly Lin

Jiyang Chen

zong jingjing

Wan Curtis

Thanks a lot. It does work.



xiyuan niu

Very helpful, thank you very much!

yong yang

Hello, when I put this function into the MATLAB2018 version of the library, I get an error: Calculate Figure WindowButtonMotionFcn. But it works on lower versions of the software.

dgreoiv clihest


Wang Howard

That's very helpful!


Great function. However, I would like to stop execution of the function without closing the figure, but have not found a clean method for doing so.
I had another issue with trying to delete/remove magnification boxes left by right-clicking or holding 'Ctrl', but was able to rectify this by replacing lines 34-37 with:

set(a2,'Tag','MagBox','UserData',[2,0.2],'Color',get(a1,'Color'),'Box','on'); % Magnification, frame size

and then adding the following code after line 92:

elseif strcmp(eventdata.Key,'backspace') || strcmp(eventdata.Key,'delete')

This adds a tag to the added Axes making it easier to identify and then deletes all magnification Axes when 'delete' or 'backspace' are pressed.

Eric Xu

Hailin Du

Great job! Thank you!

jonny huang

xj z

Ibrahim Bagci



xiaojiang wang

How to move the locked magnifying glass?


So good ! Thanks!

Guowei Yao


David Sun

That's great! Thx~

Yiyong Sun

Jee Lee


ying zhang

wonderful,thank you!

chunyang lee

Thank you!



Mojgan Rostaminia


Thank you very much!



It is really help me a lot.

nhan hanh

Thank you !!!
It helps me a lot, and I make a little changes in my code if you don't mind.
Thank you !


Thank you

li yu

Harish Babu Kankanala

Nicolai Neumann



really good~

pengxin han

Very helpfull

Ma Kang

Great job! It helps me a lot, and I make a little changes in my code.

plot(1:100,randn(1,100),(1:300)/3,rand(1,300)), grid on,
% You must complete the amplification in the figure before you run the next code.Assuming we just magnify once.
d(1).Position=[0 0.5 d(1).Position(3) d(1).Position(4)];%Move the magnified figure to the specific position
axis = d(2);
rectangle('Position',[d(1).XLim(1),d(1).YLim(1),d(1).XLim(2)-d(1).XLim(1),d(1).YLim(2)-d(1).YLim(1)],'LineWidth',2,'LineStyle','--','EdgeColor','r')%Mark the magnified part in the original figure

Carl Witthoft

Should replace "fullcrosshair" with "crosshair" because the former is unsupported in R2015 and later (possibly also in R2014)

Pengli Zhou

That's very helpful! Good work! the matlab is more powerful now

Fernando Esteves



It doesn't seem to work when the figure style is docked - any ideas why not?

To reproduce the behavior, try the following which doesn't work:
grid on;magnify
But if you go back to set(0,'DefaultFigureWindowStyle','normal'); then it works just fine.


Thank you very much!


thanks man! It's very brilliant!

Amr Hamza

Awesome !!


It was very usefull for my thesis, thank you. Great job!

Sean Archer



Nice work.
How to detach it from the current figure without closing it?


Martin Fuchs


excellent code. how do i extend this to an image inside uipanel on figure ?


Victor P

Excellent! Thanks!

Yuval Cohen


Yuval Cohen


great job^_^

in my case, the y axis is log scaled, seems just a littile modification as follows will do

set(a2,'YLim',a1_cp(2)+(1/a2_param(1))*(a2_pos(4)/a1_pos(4))*diff(get(a1,'YLim'))*[-0.5 0.5]) (in function "ButtonMotionCallback")
set(a2,'YLim',10.^(log10(a1_cp(2))+(1/a2_param(1))*(a2_pos(4)/a1_pos(4))*log10(Ylima1(2)/Ylima1(1))*[-0.5 0.5]))

Evgeny Pr

I made the following changes to work properly:

in ==> magnify>ButtonDownCallback at 39
- set(get(a2,'Children'), ...
- 'LineWidth', 2);

+ line_obj = findobj(a2, 'Type', 'line');
+ if ~isempty(line_obj)
+ set(line_obj, 'LineWidth', 2);
+ end

Evgeny Pr

Very good function! But there is a slight problem. By operation with the image in function there is an error:

>> imshow(im, []);
>> magnify;
??? Error using ==> set
There is no 'LineWidth' property in the 'image' class.

Error in ==> magnify>ButtonDownCallback at 39
set(get(a2,'Children'), ...

Thus after an error function continues to work normally at mouse moving.
I think, it is necessary to check that the object on axes is the image and to eliminate in that case change of properties of a line.



Excellent job. I guess it needs another revision to take care of some of the bugs that reveal themselves in the newer version of Matlab.

I use this function with imshow() and it gives me an error that:

There is no 'LineWidth' property in the 'image' class.
Error in ==> magnify>ButtonDownCallback at 39
set(get(a2,'Children'), ...

Of course I could easily solve it by removing that line of code. Also the Ctrl key seems to leave the box on the image. Another thing that one might want to change is to set the XTick and YTick of the second axis to [ ]:

set(a2, 'XTick', [ ], 'YTick', [ ]);

Again, excellent job!


great app, but it deletes one set of data when I use it to look at plotyy graphs. a point for consideration?


Todd Welti

I also like it, BUT doesn't seem to work well when the x axis is log based. This is pretty much all I use (frequency is normally plotted this way). I looked at the code but not sure how long it would take me to make that mod.....

mohamed Elmorsy

Excellent,Wonderful Function

Akshay B

Neat!! Thanks a lot!




This is really awesome.
I am not sure if I am doing it wrong, ctrl+click makes the box stay on fig., how can I remove it if wanted to plot another figure on same axes.


This looks extremely useful, anyone seen any problems with it on later versions of MATLAB R2007a? The zoom area is not located over the plot so cannot be seen...

Husam Aldahiyat

Superb, etc.

Jim Fallon

Excellent instruction for handling mouse button motion and using 'UserData' in a GUI. Beside learning, it saved hours for development of my own GUI.

J. Fallon

Sami Murad

good it is really helpfull

Hyway Yin

Very nice!

Dinesh Appya


Gordon Dix

Very useful code. Great work!

Yvgeny Shvarzman

If anybody can tell me if I can use this code too magnify other programs (for examle power point details) ?

Gadget Gadget

Meche Nova

I've solved this problem by setting up Units to "normalized". Works fine with several axes within one gui. Question:
how to delete a leftover window if you
occasionally hit the right mouse button?

Daphne W

This is an excellent tool! Thanks!
For those who needed it to work with images just comment out the line with LineWidth:
% set(get(a2,'Children'),...
% 'LineWidth', 2);
That solves the problem.

What I am trying to figure out now, is how to make it work inside a gui with several axes. Seems to put the magnified image at the edge of the gui for some reason. Any ideas?

Lee Jay Fingersh

Very nice app, thank you! I modified the controls slightly. Now, the arrow keys control magnification in each axis separately while the brackets control both together and the +/- keys control window size.

Shanrong Zhang

Great job! MATLAB should add it as a standard icon in their figure panel...

One small comment: if you could add one sentence "figure(f1);" after the first sentence "if (nargin == 0), f1 = gcf; end;", it should be more convenient. By doing this, the figure that is selected by user or the current one would automatically pop up to be "magnified".

Thanks for sharing !!!

Brett Shoelson

I echo Jiro's suggestion, as well as the group consensus that this is a very nice function!

Jiro Doke

Very nice! One comment:

If you could put a check before you set the LINEWIDTH, in case there are objects that doesn't have that property (e.g. image, lighting, etc). I can use this to magnify image files.

Marcelø Zeri

Very good!

John D'Errico

On a scale of 1 to 5, I'll put this at 6.

Vicky Svidenko

Wow! Excellent!

Christina Hebert

Very cool

Tsakani Lotten


Kang Dong-il

You made what I REALLY needed.. thank you

James Goodwin

very very good!

Fahad Al Mahmood

Wonderful Function Indeed! Congratulations!

Alex Podgaetsky


MATLAB Release Compatibility
Created with R14SP1
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!