version 1.13.0.0 (18.9 KB) by
Evgeny Pr

DRAGZOOM allows you to handy interactively manage the axes in figure.

**Editor's Note:** This file was selected as MATLAB Central Pick of the Week

DRAGZOOM allows you to interactively manage the axes in figure. This simple function for usable dragging and zooming of axes, rotate 3D axes, using the mouse and the keyboard shortcuts.

Features:

- Supports 2D-Plots, 3D-plots, Log plots and Images

- Supports multiple mixed (subplots) (2D, 3D) axes on one figure

- Supports X,Y,XY linking of several 2-D axes (Synchronize manage)

- Rubber Band Region Zooming

- Magnifier Tool

- Handy Cross-hair Cursor

- Handy Zooming and Draging Plots

- Handy and Fast Rotate 3D Plots

- Control Enabled Status

----------------------------------------------

Using:

dragzoom()

dragzoom(hFig)

dragzoom(hAx)

dragzoom(hAxes) % array of axes handles

dragzoom(hFig, status) % status - set enable status 'on' or 'off'

dragzoom(status)

----------------------------------------------

Control Enabled Status of DRAGZOOM:

dragzoom(hFig, 'on') % Enable DRAGZOOM for figure "hFig"

dragzoom(hFig, 'off') % Disable DRAGZOOM for figure "hFig"

dragzoom('on') % Enable DRAGZOOM for figure "GCF"

dragzoom('off') % Disable DRAGZOOM for figure "GCF"

----------------------------------------------

Interactive mode:

Available the following actions:

******************************

2D and Image modes:

******************************

- Mouse actions in 2D mode:

-- Normal mode:

"single-click and holding LB" - Activation Drag mode

"single-click and holding RB" - Activation Rubber Band for region zooming

"single-click MB" - Activation 'Extend' Zoom mode

"scroll wheel MB" - Activation Zoom mode

"double-click LB, RB, MB" - Reset to Original View

-- Magnifier mode:

"single-click LB" - Not Used

"single-click RB" - Not Used

"single-click MB" - Reset Magnifier to Original View

"scroll wheel MB" - Change Magnifier Zoom

"double-click LB" - Increase Magnifier Size

"double-click RB" - Decrease Magnifier Size

- Hotkeys in 2D mode:

'+' - Zoom plus

'-' - Zoom minus

'0' - Set default axes (reset to original view)

'uparrow' - Up or down (inrerse) drag

'downarrow' - Down or up (inverse) drag

'leftarrow' - Left or right (inverse) drag

'rightarrow' - Right or left (inverse) drag

'c' - On/Off Pointer Symbol 'fullcrosshair'

'g' - On/Off axes grid (only plots)

's' - On/Off smoothing plots

'x' - If pressed and holding, zoom and drag works only for X axis

'y' - If pressed and holding, zoom and drag works only for Y axis

'm' - If pressed and holding, Magnifier mode on

'l' - On XY / Off X,Y,XY linking 2-D axes (Synchronize manage)

'control+l' - On X linking 2-D axes

'alt+l' - On Y linking 2-D axes

******************************

* 3D Mode:

******************************

- Mouse actions in 3D mode:

"single-click and holding LB" - Activation Drag mode

"single-click and holding MB" - Activation 'Extend' Zoom mode

"single-click and holding RB" - Activation Rotate mode

"scroll whell MB" - Activation Zoom mode

"double-click LB, RB, MB" - Reset to Original View

- Hotkeys in 3D mode:

'+' - Zoom plus

'-' - Zoom minus

'0' - Set default axes (reset to original view)

'uparrow' - Rotate up-down

'downarrow' - Rotate down-up

'leftarrow' - Rotate left-right

'rightarrow' - Rotate right-left

'ctrl'+'uparrow' - Up or down (inrerse) drag

'ctrl'+'downarrow' - Down or up (inverse) drag

'ctrl'+'leftarrow' - Left or right (inverse) drag

'ctrl'+'rightarrow' - Right or left (inverse) drag

'1' - Go to X-Y view

'2' - Go to X-Z view

'3' - Go to Y-Z view

'v' - On/Off Visible Axes

'f' - On/Off Fixed Aspect Ratio

'g' - On/Off Visible Grid

----------------------------------------------

Examples:

% 2D

x = -pi:0.1:pi;

y = sin(x);

figure; plot(x, y);

dragzoom

% Image

I = imread('cameraman.tif');

figure; imshow(I, []);

dragzoom

% 3D

figure;

[X,Y] = meshgrid(-3:.125:3);

Z = peaks(X,Y);

meshc(X,Y,Z);

axis([-3 3 -3 3 -10 10]);

xlabel('x');

ylabel('y');

zlabel('z');

dragzoom;

figure;

x = -pi*2:0.1:pi*2;

y1 = sin(x);

y2 = cos(x);

hax1 = subplot(2,1,1); plot(x,y1, '.-r')

hax2 = subplot(2,1,2); plot(x, y2, 'o-b')

dragzoom(hax1); % manage only axes 1

Evgeny Pr (2020). DRAGZOOM - Drag and zoom tool (https://www.mathworks.com/matlabcentral/fileexchange/29276-dragzoom-drag-and-zoom-tool), MATLAB Central File Exchange. Retrieved .

Created with
R2008b

Compatible with any release

**Inspired:**
Dynamic Low, High and Band pass filter, Apply Standard+ Keyboard Shortcuts Keys to a Figure Window, stackobj, Cluster generator, Lunar High precision Orbit Propagator

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

Start Hunting!Create scripts with code, output, and formatted text in a single executable document.

Snow SummerWhat a wondfull tool! Actully I need a customized signal labeler, I think this tool can be modified as a good labeler which can be faster than MATLAB Signal Labeler APP.

alok pandeyThis is really cool contribution. Highly recommended and appreciated!

marco taruselliThe best function I've never tried!

esivalinpThis is a fantastic kit, hope the new version will support the datetime axis. Otherwise, I can only try to write it myself.

Simon PartenCouldn't get it to work on a stem plot with datetime x axis. Any hints?

Mohammad Askari***** QUICK FIX FOR ZOOMING ON HANDLES WITH CONTAINER PARENTS *****

This tool is by far the best interactive 2D/3D figure control widget I have found in terms of speed and performance. It is nearly flawless. As long as your axes are direct children of the figure window, the zoom option works fine. But, I was personally developing a GUI and 3D zooming was not functioning correctly. So, in case your axes is placed in a container (other than the main figure handle), you may come across this issue. I made a quick fix to this problem and thought to share it here. Scroll to GetCursorCoordOnWindow function (line 2292) and add the following lines to the function:

if strcmp(hAx.Parent.Type,'figure')

containercrd = [0 0];

else

containerabspos = getpixelposition(hAx.Parent,true);

containercrd = containerabspos(1:2);

end

Also replace the following lines:

x = crd(1);

y = crd(2);

with this:

x = crd(1) - containercrd(1);

y = crd(2) - containercrd(2);

___________________________________

Here's an example to what I'm talking about. Run the following code before and after applying the fix:

hfig = figure('position',[100 100 1000 500]);

container1 = uipanel('parent',hfig,'position',[0 0 0.5 1]);

container2 = uipanel('parent',hfig,'position',[0.5 0.2 0.5 0.8]);

hax = axes('parent',container2);

x = -pi*2:0.1:pi*2;

y = sin(x);

n = 2^5-1;

[x,y,z] = sphere(n);

surf(x,y,z);

dragzoom();

Mohammad AskariGasztonMarco FurlanGuojin FengBDCKangcheng HouMarko UsajReally good work!

Can anyone help me how to get/store the coordinates which are shown after you enable 'c' - On/Off Pointer Symbol 'fullcrosshair'? That would be awesome! Thanks for anyone help and time!

Marco FurlanExcellent work!

Many thanks for sharing, it improves my work efficiency a lot

Gustav PotgieterThanks, great work! I have opened your "test_subplot_2D" file and it works perfectly. I was just wondering, can all three subplots be zoomed/panned etc. at the same time?

You have included: " 'l' - On XY / Off X,Y,XY linking 2-D axes (Synchronize manage) " under "2D and Image modes: " in this page, this might be the answer? But, my understanding to make use of it may be a bit off?

I see the creator of DRAGZOOM, Evgeny Pr, has not been active on his account for a year. Maybe someone else could help? Thanks.

Lorenzo GasperiIt woul be fantastic if it would work with yyplot!

Error using matlab.graphics.axis.Axes/set

While setting the 'YLim' property of 'Axes':

Value must be a 1x2 vector of numeric type in which the second element is larger than the first and may be Inf

StefanoM64Wonderful!

Steven SagonaIs there a way that I can zoom in just with respect to the x or y axis? (that is, keep the x axis the same and zoom in on the y-axis)

Also, if I graph two figures in the same plot, is there a way I can independently zoom in-and-out each plot? Also is there a way I can zoom on each of them together? (and can I incorporate these commands when zooming in to the previously asked-for ability to zoom in with only the x-axis?)

Christine Sai-HalaszConstantine PapakonstantinouArthur QueirozJoe PassmanEvgeny,

This is a fantastic tool. Does it have any functionality to save the current figure after the manipulations are done? I basically want to do this because I want to further post-process an image after manipulating with your tool.

Thanks!

Joe

Joe PassmanphyattThis is an excellent tool. I made the zoom/pan/position persistent and make a gist showing how to do it.

https://gist.github.com/peteristhegreat/879c99dcfc569770dc44

Keep up the great work!

Evan LuoThis tool is great!! I was using this tool to trace the coordinate on my 2D plot created from GUI. The 'c' - On/Off Pointer Symbol 'fullcrosshair' works great without showing my legend. Whenever I show my legend, the mouse icon disappear as well as the cross pin. How would I fix that?

Johannes KorsaweEvgeny,

maybe i was wrong.

Somehow my figure, where i wanted to use dragzoom, had a saved property "UserData" which referred to an 1x0 empty Graphics array (?wherever this came from?). As you also use this property, i just had to empty UserData (by replacing the above value with []) to make dragzoom work.

It seems, that 2014b still respects a lot of the old notation.

Congrats to this great submission!

Evgeny Pr@Johannes Korsawe

Since R2014b the graphics system has been rewritten via object-oriented approach. The "dragzoom" function must be fully rewritten for support R2014b.

I do not know when I can do it. I do not have access to Matlab R2014b now.

Forks welcome. :)

Johannes KorsaweDoes not work with Matlab R2014b. Any plans to migrate?

MichaelVery nice submission! Is there an easy way to add the ability to make the magnifier sticky (i.e. it doesn't disappear when you let go of the "m" key)? And the follow-on question would be could you then have a second magnifier box with the next "m" press? I'm thinking something along the lines of shift+m to make a new sticky magnifier and then ctrl+m to delete all magnifiers (or something like that).

Jonathan ListerI really like this submission not just for all the handy interactivity it provides, but also for the great handling of the windowfcn callbacks. Seeing how you handled that was worth the download. I did notice one small bug. When you call dragzoom on an axes that is actually a child of a panel using the scroll button doesn't work. Other than that, great submission.

JulianThanks Evgeny, this is a really useful tool. Although I downloaded it a while ago I have only recently started to use it regularly.

It feels like a more intuitive way interacting with a plot, dragging and zooming like in Google Maps. This is much more natural (and quicker) for me than clicking the pan and zoom tools. I think this facility should be built into MATLAB graphics (which are badly in need of a revamp).

Changshun DengI like this tool! Great job!

Evgeny Pr@Terence Ryan,

GINPUT function takes away control on itself (executes its callbacks for interact events). I think, will be problematic to achieve native GINPUT and DRAGZOOM collaboration. :(

Evgeny Pr@Ippei Kotera,

Unfortunately, the simple solution to this problem I don't know. This problem appears in case of set property "String" for graphics object "text".

For example (in dragzoom PointerCrossUpdate func):

set(mPointerCross.htext(left), 'String', yValStr)

Terence RyanThis is a great function and it works amazingly. It is almost perfectly what I need. Maybe someone can help with my problem... I would like to use this function to zoom and drag on my plots, but also use ginput to select points on my plot. Is this possible?

Thanks

Ippei KoteraVery nice tool. One small thing I noticed is when I use OpenGL renderer, as in the following example, the crosshair disappears. Do you think you can find a way around? I'm using 2010a/Windows 7 64 bit.

h=figure;

axes;

set(h, 'Renderer', 'OpenGL');

dragzoom();

Thanks.

Aurelien QueffurustReally Excellent!

Evgeny PrHi Giacomo,

Thanks!

Yes, you can do this. I tried to do so, but something didn't work properly, so I retracted this decision in favour of that is done in matlab-function ZOOM.

GiacomoHi,

really good function...

would it be easy to modify it to have 3D drag of the axes limits instead of moving the axes object in the figure?

(like the 2D case)

Thank you

Evgeny PrHi Matthew Arthington,

Thanks!

This is a very good idea, but unfortunately there is dependence on the external function. But overobj2 is small function, it can be added inside dragzoom. :)

Matthew ArthingtonP.S. To make it work I also had to change line 22 of overobj2 from

fig = get(0,'PointerWindow');

to

fig = gcf;

Matthew ArthingtonHi Evgeny,

This is a great function, thank you.

I've added a modification to mine so that it doesn't change the figure name, but instead it changes the mouse cursor when it hovers over the axes. I added:

hAxesTemp = overobj2('type','axes');

if ~isempty(hAxesTemp)

set(gcf,'Pointer','fleur');

else

set(gcf,'Pointer','arrow');

end

to the end of the WindowButtonMotionCallback2D function and I'm calling overobj2 from http://undocumentedmatlab.com/blog/undocumented-mouse-pointer-functions/

Dave LsEvgeny Pr@Dave Ls,

I am glad that all works! Thanks for a use my function.

Dave LsIt is my mistake. I used "save link as ..." to save the file.

It is working now. Thanks a lot for the function.

Evgeny PrPlease, specify the number of line.

Dave LsHi Evgeny,

Not HotKey, I got error message when I run the file. It said "unexpected matlab operator".

Thanks a lot for your reply.

Evgeny Pr@Dave Ls,

Hello.

I not so well understand your problem. Hot keys don't work?

Dave LsThis is the function I am looking for. But somehow I can not make it work. It doesn't recognize letters < and " in HTML, { in for loop. I am using R2009a. What is wrong? Please help. Thanks

BenThis is a great function!

PiotrMohammed El-SaidEvgeny Pr@Nate,

Thank You! I understand that you want to do. It is necessary to think how to do this in DRAGZOOM.

Nate JensenEvgeny Pr,

First off, I love this function, great job! I am wondering though how to incorporate this with other callbacks that are normally assigned to the pan and zoom action post and pre callbacks?

I am plotting a lot of data and Matlab, or my computer, has trouble plotting all of the data and panning around or zooming in smoothly. So what I am doing is plotting a subset of the data and after I pan or zoom, I call the 'ActionPostCallback' to replot the data.

Do you have any idea how I could incorporate the 'ActionPostCallback' with your function dragzoom?

Thanks,

Nate

MartinFinally get it running! Great work!

JoachimSherifGreat work.

RiccardoGreat Job.

Thanks for sharing

MilosI think that the most universal number format would be MATLAB's shortG.

Wolfgang SchwanghartExcellent tool!

Evgeny Pr@Milos,

Thank You!

Not yet, but I think I like to make a universal format for numbers.

Interested in any suggestions! :)

MilosThis is great! Nice job, Evgeny. Is there a way to control number format when using cross-hair?

MilosEvgeny Pr@Ion,

Thanks!

I'll check this possibility

@Jiro Doke,

Yes, in latest version I fixed of "Statistics Toolbox" dependency.

@Martin,

Please, update DRAGZOOM to the latest version.

jiroOh, it seems like your most recent update (8 Dec 2010) fixed this toolbox dependency. You created a function "range" inside.

@Martin,

Try downloading the latest version.

jiro@Martin,

"range" is a function from Statistics Toolbox, so you need that toolbox to run it. You could replace all occurrences of "range" with your own implementation. Range is simply max(val) - min(val).

@Evgeny,

You should check "Statistics Toolbox" as a required toolbox for this submission.

MartinHi, I have some problems getting this running. I have used the 2D example just as written in the description and then pressed the "+" key. What I get is following

??? Undefined function or method 'range' for input arguments of type 'double'.

Error in ==> dragzoom>GetCurrentZoomAxesPercent at 1196

curentZoomX = range(defaultXLim) * 100 / range(xLim);

Error in ==> dragzoom>UpdateCurrentZoomAxes at 1168

[curentZoomX, curentZoomY] = GetCurrentZoomAxesPercent(xLim, yLim);

Error in ==> dragzoom>ZoomKeys at 1048

UpdateCurrentZoomAxes();

Error in ==> dragzoom>WindowKeyPressCallback2D at 487

ZoomKeys('plus');

??? Error while evaluating figure WindowKeyPressFcn

I'm using Matlab 2010a. Must be a mistake by myside because all other have no such problems! Do you know whats wrong?

IonNice job, Evgeny Pr !

For apply the zooms and grab to more than one subplot at the same time, have you tried Linkaxes(handles) ?? It should work

Yuri KSuper! Should be build into MATLAB. I'd vote. It would be nice to have a little mode-sensitive help window opening by a key (h or ?, for example).

DmitryThis is wonderful tool! Thanks!!! However it 3D mode I have probelms in activation of Rotate mode (I press and hold right mouse button but I select only region of zoom). I have also problems with hotkeys, f.e. '1' '2' and '3' does not change view as it is explained above.

Etienne O'BrienIt now works. Thank you.

Etienne

Evgeny PrEtienne O'Brien,

This strangely...

Try commenting out the function CreateFigure (lines 683-691) and commenting line 312.

Etienne O'BrienYes, I think I am editing the file which runs. When I type 'which('dragzoom') this is what I get:

C:\Users\Administrator\Documents\MATLAB\dragzoom.m

This is where all my working m files are kept.

Etienne

Evgeny PrEtienne O'Brien, You are sure you are editing the same file that runs?

Command which('dragzoom') show full path to file, which runs.

Etienne O'BrienThanks Evgerny,

Unfortunately, this solution did not work.

a. I replaced "~" with several different letters of the alphabet but this messages was repeated.

E.g. function CreateFigure(src, a)

>> dragzoom

??? Error: File: dragzoom.m Line: 683 Column: 32

Unexpected MATLAB operator.

b. I then deleted this operator and the following happened:

function CreateFigure(src)

>> dragzoom

??? Error: File: dragzoom.m Line: 683 Column: 32

Unexpected MATLAB operator.

Evgeny PrEtienne O'Brien, you have an older version of MATLAB, that does not support operator "~" in function arguments. Replace "~" operator in line 683 at any other letter.

Etienne O'BrienHi Evgeny,

I determined to make your code work but I'm stuck! I'm a novice MATLAB user so my problem may be simple. I downloaded and saved your file to the same folder where my other m.files are. I created a Figure (a simple x, y plot) and entered 'dragzoom'. The following is what happened.

>> dragzoom;

??? Error: File: dragzoom.m Line: 683 Column: 32

Unexpected MATLAB operator.

Could you advise please?

Evgeny Prkarlo gonzales, at this time this is not supported. I'll think about how to do it to be convenient for use.

karlo gonzalesThanks Evgeny Pr, for reply.

I don't want to use it for a complex case, for example;

consider two subplots that contain a function and its derivative

subplot(2,1,1) plot (f(x,t))

subplot(2,1,2) plot (df(x,t)/dt)

So, for this 2-D graphs, can we apply the dragzoom function at the same time for both subplots?

Evgeny PrThanks for your feedback, karlo gonzales!

If the axes will contain different plots (3D or 2D), different limits, etc, how to manage them at the same time in this case?

karlo gonzalesWonderful job, Evgeny Pr!

Is there anyway to use this function, simultanously for subplots?

For example, change axes, zoom in/out.., happens at the same time for both subplot(2,1,1) and subplot(2,1,2).

Thansk again

LaurentVery nice work.

It would also be nice to disable it

Evgeny PrI'm currently doing support logarithmic plots. I completely forgot about it. It's unpleasant when something doesn't work as expected. :(

Evgeny PrDoug Harriman, Thanks for your feedback.

This possibility has been before, but I removed it. This should work as in the ZOOM function: "zoom on", "zoom off"?

Doug HarrimanExcellent work. Is it possible to disable this mode for an axes object once it has been enabled?

LaurentEvgeny PrBen, thanks for your feedback.

BenVery nice function! Matlab should take this.

SophieMega cool! That works!! Thanks!!

SophieCool! Thanks!

Evgeny PrSophie, I've fixed the problem and submit files on Fileexchange. Should soon be updated.

SophieThanks. hope you will find a solution:)

Evgeny PrThanks, Sophie!

This should work on this scheme:

figure;

hax1 = subplot(...);

plot(...)

hax2 = subplot(...);

plot(...)

hax3 = axes();

dragzoom(hax1); % for manage only Axes 1

But for some reason doesn't work. Always selects the last axes.

I'll fix this bug soon.

SophieThis is a great job. However, would it be possible to set this dragzoom option only on a given axis of the figure? Because if I have several axes, I would like this dragzoom option to work only if my mouse is over the image in this axis precisely...?