Code covered by the BSD License  

Highlights from
akZoom

4.8

4.8 | 5 ratings Rate this file 27 Downloads (last 30 days) File Size: 11.5 KB File ID: #41426
image thumbnail

akZoom

by

 

22 Apr 2013 (Updated )

Zooming and panning with the mouse in 2D plots. Supports plotyy, loglog, subplot, etc.

| Watch this File

File Information
Description

allows direct zooming and panning with the mouse in 2D plots.

 Default mouse button functions (can be customized, see below):
        Scroll wheel: zoom in/out
   Left Mouse Button: select an ROI to zoom in
 Middle Mouse Button: pan view
  Right Mouse Button: reset view to default view

SYNTAX:
  akZoom
  akZoom(handles)
  akZoom(handles, mButtons_ZoomPanReset)
  akZoom(mButtons_ZoomPanReset)

DESCRIPTION:
  akZoom() activates mouse control for all axes-objects in the current figure.

  akZoom(handles) activates mouse control for the axes given by handles.
    handles can be:
    a) a single axis handle.
    b) an array of axis handles. In this case all axes will be linked,
       i.e. panning and zooming in one of the axes will affect the others as well.
    c) a cell array consisting of axis handles and/or arrays of axis
       handles. In this case mouse control is activated for all axes inside
       the cell but linking is only activated for axes in a commong array.
    d) 'all': activates mouse control for all axes in all open figures.
       The axes will not be linked except for axes belonging to the same
       plotyy plot.
    e) 'all_linked': activates mouse control for all axes in all open
       figures and links all axes.

  akZoom(handles, mButtons_ZoomPanReset) and akZoom(mButtons_ZoomPanReset)
    will use akZoom with customized mouse buttons.
    mButtons_ZoomPanReset can be:
    'lmr', 'lrm', 'mlr', 'mrl', 'rlm' or 'rml'
    where the letters stand for the buttons left, middle and right.
    So 'lmr' means left=ROI-zoom, middle=pan and right=reset.
    Note: If you want to use a certain mouse button pattern as default,
    just change mButtons_ZoomPanReset_default in the "Settings" section
    below.

EXAMPLES:
%% Simple Plot
    figure
    plot(10:24,rand(1,15));
    akZoom();

%% Subplots (independent axes)
    figure
    for k = 1:4
      y = rand(1,15);
      subplot(2, 2, k);
      plot(y);
    end
    akZoom();

%% Subplots (mixture of linked and indipendent axes)
    figure
    ax = NaN(4,1);
    for k = 1:4
      y = rand(1,15);
      ax(k) = subplot(2, 2, k);
      plot(y);
    end
    akZoom({[ax(1),ax(3)],ax(2),[ax(3),ax(4)]});

%% Different figures (linked)
    figure;
    im1 = imread('peppers.png');
    imshow(im1)
    ax1 = gca;
    figure
    im2 = rgb2gray(im1);
    imshow(im2)
    ax2 = gca;
    akZoom('all_linked');

%% More examples in the file "akZoom_examples.m"

KNOWN BUGS
  No bugs at the moment. :) If you find one, please report.

Acknowledgements

Mouse Friendly Figure inspired this file.

This file inspired Ui Signal Builder2.

Required Products MATLAB
MATLAB release MATLAB 7.13 (R2011b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (9)
29 May 2014 Benoit Botton

Hi alexander,

this is a very good piece of code, so useful. thank you for sharing.

I ran into the following issue:
in a GUI I built, my axes are within a uipanel. so I kept getting this error:
" Error using hg.uipanel/set
The name 'WindowScrollWheelFcn' is not an accessible property for an instance of class 'uipanel'. "

my quick fix was to add the following lines at the end of the parse_handles function:

for nn = 1:length(h_fig)
while ~strcmpi( get( h_fig(nn), 'Type'), 'figure')
h_fig(nn) = get(h_fig(nn), 'Parent');
end
end

some quick testing shows it works

10 Dec 2013 Alexander Kessel

Hi Vincent,
Thanks for your idea. It should be feasible. I'll think about including it in the next release.

04 Dec 2013 Vincent

Is it easy to add the feature with linked glasses to only keep x linking. So that you can pan/zoom in a subplot and only the x limits in the other linked subplots change?

05 Nov 2013 arnold

great stuff!
Always wonder why Mathworks can't seem to manage to include such "basic" functionality, puzzling.

09 Oct 2013 Shahab  
08 Oct 2013 Alexander Kessel

Hi Francesco,
Thanks for the feedback. I just uploaded the next version which will be published as soon as the MathWorks team has finished reviewing it.

Regarding the zoom/pan bug: Yes I have the same problem and on the way for a solution I found that many people have similar issues of loosing window focus after some operation. Sometimes they could find a workaround for this Matlab behaviour but for the case where you delete a patch object (like in akZoom) I couldn't find a solution. However, I wanted to get rid of the patch object anyway, so I replaced it with a rectangle: voilĂ !

Regarding customizing the mapping of the mouse buttons: you can do this now easily in the "Settings" section of the akZoom function. I guess everyone has a certain scheme he/she wants to work with but there is no need to change this really at runtime. Correct me if I'm wrong.

06 Oct 2013 Francesco Montorsi

Thanks for this useful submission!
Just one problem I've found so far on my MATLAB r2013a: after a zoom by left-click (zoom by creating a rectangle), I cannot used anymore the scroll wheel to zoom in or out... do you have the same issue?

Also it would be very nice to allow the caller to specify the mapping of the mouse buttons: e.g., I would like to pan with the left mouse button and to zoom by a rectangle with the middle button... I can modify that in the code myself, but having the function configurable at runtime would be nice IMHO :)

16 Jun 2013 Alexander Kessel

Hi Felix,

Thanks for your comment and rating!

What you could do is to create a wrapper for the MATLAB-plot function:

function h = myPlot(varargin)
h = plot(varargin{:});
akZoom(get(h,'Parent'));
end

Save this function in a folder of your choice and add it to the Matlab-Search-Path.

Now you can use the function myPlot the same way you would use "plot" and akZoom is included automatically.

12 Jun 2013 Felix

thanks, works very well!
I wonder, is there a way to automatically have it run for every plot, without typing akZoom every time...

Updates
07 May 2013

*bugfixes
*out-of-the-box functionality with subplots and (multiple) plotyy-axes
*arbitraty linking between different axes can be easily set up

08 Oct 2013

-zooming: replaced patch by rectangle -> faster, visually not dependent on renderer, bug fixed that prevented scroll wheel events after zoom
-mapping of mouse buttons can be customized
-easy usage with 'all' and 'all_linked' (works now with plotyy)

09 Oct 2013

updated Description

04 Nov 2013

*supports now loglog & semilog
*mouse buttons customizable
*unsensitive to data markers & legends
*checks for valid axis limits (log!)
*automatically callable for 2D plots

Contact us