File Exchange Pick of the Week

July 2nd, 2009

linkzoom

Bob's pick this week is linkzoom by Carlos Adrian Vargas Aguilera.

The following plot shows temperature versus depth in two sets of units.

Z = linspace(0,150)';           % Depth in meters
TC = -tanh((Z-30)/20)+23;       % Temperature in °C
dc2df = @(dc) (9/5)*dc + 32;    % °C->°F
mt2ft = @(mt) mt/0.3048;        % meters->feet

ax(1) = axes('YDir','reverse',...
             'Box','off',...
             'position',[0.13 0.11 0.77 0.78]);
line(TC,Z,'parent',ax(1))
axis tight
xlabel('Temperature, °C')
ylabel('Depth, m')
ax(2) = axes('Position',get(ax(1),'position'),...
             'HitTest','off',...
             'XAxisLocation','top',...
             'YAxisLocation','right',...
             'YDir','reverse',...
             'XLim',dc2df(get(ax(1),'XLim')),...
             'YLim',mt2ft(get(ax(1),'YLim')),...
             'Color','none');
xlabel(ax(2),'Temperature, °F')
ylabel(ax(2),'Depth, ft')
linkzoom(ax)

The different units of measure are handled using a second axes. If you want to zoom and pan the plot, by default the two axes would not stay synchronized. MATLAB offers a very useful linkaxes command. That works great when plots share common X and/or Y coordinate values. In this case linkaxes would do the wrong thing. So Carlos created linkzoom. Once turned on, you can pan and zoom and both measurement scales stay in lock step. Download the submission and run this code in MATLAB to see for yourself.

Comments?


Get the MATLAB code

Published with MATLAB® 7.9

One Response to “linkzoom”

  1. Andres replied on :

    Great!!
    I just checked it works right away with addaxis, http://www.mathworks.com/matlabcentral/fileexchange/9016 , too, for those who need a good multi-y-axis plot function but don’t like to touch their original zoom function.

    x = 0:.1:4*pi;
    figure
    plot(x,sin(x),'r-');
    xlim([0,4*pi])
    addaxis(x,sin(x-pi/3),'Color',[0 0 0.7]);
    addaxis(x,sin(x-pi/2)/4,[-0.5 0.5],'Color',[0.4 0 0]);
    grid on
    
    linkzoom
    % now zoom around...
    

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • Doug: Ben, The name is in the name field as you saw in the video: files(1).name will give you just the name of the...
  • Ben: Hey, Thank you for the tutorial but I must ask, once you have the files required in the array, how do you parse...
  • jiro: Nisa, I’m not familiar with the Psychtoolbox. It’s not a toolbox developed by the MathWorks. As far...
  • nisa: Hi, I have just started to learn to use mathlab can this myaa codes be used with mathlab 7.5.0(R2007b)using...
  • Doug: @Rupa, Could you do this algorithm and then remove the two points and run again? Not efficient, but could be...
  • Doug: From the convhull doc http://www.mathworks .com/access/helpdesk /help/techdoc/ref/co nvhull.html xx = -1:.05:1;...
  • Mark: Very nice function, indeed, thanks for that. But why does’t MATLAB freeze the colormaps for subplots...
  • Steve L: MAuricio, From the description it sounds like the features variable is a cell array that itself contains a...
  • Fred: Doug, I have a set of coordinates for the perimeter of an object in random order. I want to plot these using...
  • Rupa: Hi, sorry to digress but I have a similar problem, I think. I would like to find the three (or n) smallest...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.