Code covered by the BSD License  

Highlights from
choose_transect_limits

image thumbnail
from choose_transect_limits by Kevin Bartlett
GUI for choosing time limits of oceanographic transects

latlonlims(varargin)
function [] = latlonlims(varargin)
%
% latlonlims.m--Resets map axes limits after zooming to have same scale in
% both x and y directions.
%
% Called by Mathworks' fireActionPostCallback.m with input arguments
% "figHndl" (figure handle) and "evd" handle to the event object (a
% structured variable with field evd.Axes containing the handle of the axes
% being zoomed).
%
% LATLONLIMS can also be called from the command line with no input
% arguments, in which case the current figure and axes are assumed.
%
% Syntax: latlonlims(<figHndl,evd>)
%
% e.g.,   plot([-124 -120],[48 48.5],'-o');
%         latlonlims; % (initial setting of correct aspect ratio)
%         zoomHndl = zoom(gcf);
%         set(zoomHndl,'Enable','on','ActionPostCallback',@latlonlims);

% Developed in Matlab 7.6.0.324 (R2008a) on GLNX86.
% Kevin Bartlett (kpb@uvic.ca), 2008-06-16 11:23
%-------------------------------------------------------------------------

% Allow user to call from command line with no input arguments.
if nargin == 0
    figHndl = gcf;
    evd = [];
    evd.Axes = gca;
else
    % As called from fireActionPostCallback.m.
    figHndl = varargin{1};
    evd = varargin{2};    
end % if

axHndl = evd.Axes;
xlims = get(axHndl,'xlim');
ylims = get(axHndl,'ylim');

centralLatitude = mean(ylims);
centralLongitude = mean(xlims);

% Determine post-zoom map limits in nautical miles.
yRangeNM = abs(diff(ylims)*60);
xRangeNM = abs(diff(xlims)) * cos(centralLatitude*pi/180)*60;

% Use the same scale in both x (longitude) and y (latitude) directions.
% Will use the largest of the two ranges found so that area displayed may
% be larger, but never smaller than specified by user.
rangeNM = max([xRangeNM yRangeNM]);

% Convert back to latitude and longitude.
latRange = rangeNM/60; % degrees
newLat = centralLatitude +  [-latRange/2 latRange/2];

lonRange = (rangeNM/cos(centralLatitude*pi/180))/60; % degrees
newLon = centralLongitude +  [-lonRange/2 lonRange/2];

set(axHndl,'xlim',newLon,'ylim',newLat);

Contact us at files@mathworks.com