MATLAB: How to display DMS (deg,min,sec) x- and y-labels from decimal degress dataset

dmsxlabel

Hi!
I have a matrix of latitude and longitude in decimal degrees, for height data z. I am using countourf to plot this data. However when I plot the map, I would like the x- and ylabels to show dms and not decimal degrees. For example I would like '60°5N' and '7°5E', not '60.0833' and '40.0833'. This is what I have so far:
figure
[C,h]=contourf(lon,lat,z,clevel);
set(h,'Linewidth',.5,'color',[.7 .7 .7]);

Best Answer

ax = gca;
xt = get(ax, 'XTick');
yt = get(ax, 'YTick');
nxtick = length(xt);
nytick = length(yt);
EorW = repmat('E', 1, nxtick);
EorW(xt < 0) = 'W';
NorS = repmat('N', 1, nytick);
NorS(yt < 0) = 'S';
absx = abs(xt);
xDeg= floor(absx);
xMin = floor( (absx - xDeg) * 60 );
absy = abs(yt);
yDeg = floor(absy);
yMin = floor( (absy - yDeg) * 60 );
degsym = char(176); %degree symbol is char(176)
xlabs = strcat( sprintfc('%d', xDeg), {degsym}, sprintfc('%d', xMin), num2cell(EorW) );
ylabs = strcat( sprintfc('%d', yDeg), {degsym}, sprintfc('%d', yMin), num2cell(NorS) );
set(ax, 'XTickLabel', xlabs, 'YTickLabel', ylabs)