function GeogrCoordLabel(type, incr, varargin)
%
% Function for labeling an axis with geographical coordinates
%
% PARAMETERS
% ----------
% type: 'deg', 'min' or 'sec' -> Used to set the smallest label unit
% incr: <= 60 -> Increment of the smallest label unit
% additional:
% 'label': Label with one letter for North, South, East, West (in this order), e.g. 'NSEW' or 'NSOW'
% 'origin': Define origin for labeling, needed if axis starts at 'odd' coordinate
%
% EXAMPLES
% --------
% figure; set(gca,'xlim',[101 107], 'ylim',[42 46.3]); GeogrCoordLabel('deg', 1)
% figure; set(gca,'xlim',[101 102.1], 'ylim',[41.1 41.9]); GeogrCoordLabel('min', 15)
% figure; set(gca,'xlim',[-101.02 -101.], 'ylim',[41.13 41.15]); GeogrCoordLabel('sec', 15)
% figure; set(gca,'xlim',[-.005 .005], 'ylim',[-.005 .005]); GeogrCoordLabel('sec', 5, 'label', 'NSOW')
% figure; set(gca,'xlim',[-.006 .008], 'ylim',[-.003 .009]); GeogrCoordLabel('sec', 5, 'label', 'NSEW')
% figure; set(gca,'xlim',[-.006 .008], 'ylim',[-.003 .009]); GeogrCoordLabel('sec', 5, 'origin' ,[-1 -1], 'label', 'NSEW')
%
% General
% -------
% Version 1.0 (24.11.2004): Just finished it, no more bugs found.
% Version 1.01 (06.12.2004): Corrected spelling of 'axis' to 'axes' (Line 32, Argument checking), sorry ...
% Version 1.1 (01.08.2005): New switch for setting an origin for labeling different to the axis minimum (compare example 5 and 6).
% Call to function has been modified compared to Version 1.01 (see example 4-6)
% Labeling with fixed format (e.g. 1101' instead of 111')
% axis box is switched on and axis layer is set to 'top'
% Author
% ------
% Sebastian Hlz, TU Berlin, Germany
% hoelz@geophysik.tu-berlin.de
% Argument checking
if(length(findobj(0,'type','axes'))==0); disp('No axes object available!'); return; end
if nargin < 2; return; end
if incr>60; return; end
xlim = get(gca,'xlim');
ylim = get(gca,'ylim');
if any(abs(xlim)>180) | any(abs(ylim)>90); disp('This is not an axis containing geographical coordinates !!!'); return; end
switch lower(type)
case 'deg'; delta = incr;
case 'min'; delta = incr/60;
case 'sec'; delta = incr/3600;
otherwise
disp('Valid values for type are: ''deg'',''min'',''sec''')
return
end
% Reading optional parameters
for i=1:2:length(varargin)
if strcmpi(varargin{i},'origin'); origin=varargin{i+1}; end
if strcmpi(varargin{i},'label'); label=varargin{i+1}; end
end
if ~isstr(label) | length(label)~=4; label=''; end
if ~isnumeric(origin) | length(origin)~=2; origin=[]; end
% Setting ticks and getting labels from 'GetLabel-function'
if ~isempty(origin);xlim(1)=origin(1); ylim(1)=origin(2); end
xtick = (floor(xlim(1)):delta:ceil(xlim(2)))';
xtick = xtick(find(xtick>=xlim(1) & xtick<=xlim(2)));
xlab = GetLabel(type,xtick);
ytick = (floor(ylim(1)):delta:ceil(ylim(2)))';
ytick = ytick(find(ytick>=ylim(1) & ytick<=ylim(2)));
ylab = GetLabel(type,ytick);
% Alterning Labels using 'NSEWLabel-function' to NSEW-style if 'lab' is specified
if isstr(label) & length(label)==4
xlab = NSEWLabel(xlab, label([4,3]));
ylab = NSEWLabel(ylab, label([2,1]));
end
set(gca,'xtick',xtick,'ytick',ytick,'xticklabel',xlab,'yticklabel',ylab,'box','on', ...
'layer','top','tickdir','out')
% ------------------------------------------------
% Function for generating (xy'z'') - style labels
function label = GetLabel(type,tick)
for i=1:length(tick)
if tick(i)<0; vorz = '-'; else vorz = ''; end
tick(i)=abs(tick(i));
deg = fix(tick(i));
min = floor((tick(i)-deg)*60);
if min == 60; deg = deg+1; min = 0; end
sec = round((tick(i)-deg-min/60)*3600);
if sec == 60; min = min+1; sec = 0; end
switch lower(type)
case 'deg'
label{i} = [vorz num2str(deg) ''];
case 'min'
label{i} = [vorz num2str(deg) '' num2str(min,'%02i') ''''];
case 'sec'
label{i} = [vorz num2str(deg) '' num2str(min,'%02i') '''' num2str(sec,'%02i') ''''''];
end
if deg==0 & min==0 & sec==0; label{i}='0'; end % Comment this, if you don't want the special case (0) to be handled
end
% ---------------------------------------------------------------
% Function for changing label style: e.g. (xy'z'') -> (xy'z''N)
function label = NSEWLabel(label,bez)
for i=1:length(label)
if strcmp(label{i}(1),'-')
label{i} = [label{i}(2:end) bez(1)];
elseif strcmp(label{i},'0') % Comment this, if you don't want the special case (0) to be handled
else
label{i} = [label{i}(1:end) bez(2)];
end
end