degrees2dms - Convert degrees to degrees-minutes-seconds

Syntax

DMS = degrees2dms(angleInDegrees)

Description

DMS = degrees2dms(angleInDegrees) converts angles from values in degrees which may include a fractional part (sometimes called "decimal degrees") to degree-minutes-seconds representation. The input should be a real-valued column vector. Given N-by-1 input, DMS will be N-by-3, with one row per input angle. The first column of DMS contains the "degrees" element and is integer-valued. The second column contains the "minutes" element and is integer valued. The third column contains the "seconds" element, and can have a nonzero fractional part. In any given row of DMS, the sign of the first nonzero element indicates the sign of the overall angle. A positive number indicates north latitude or east longitude; a negative number indicates south latitude or west longitude. Any remaining elements in that row will have nonnegative values.

Example

format long g
angleInDegrees = [ 30.8457722555556; ...
                  -82.0444189583333; ...
                   -0.504756513888889;...
                    0.004116666666667];
dms = degrees2dms(angleInDegrees)

dms =
                30                50          44.7801200001663
               -82                 2          39.9082499998644
                 0               -30          17.1234500000003
                 0                 0          14.8200000000012

% Convert angles to a string, with each angle on its own line.
nonnegative = all((dms >= 0),2);
hemisphere = repmat('N', size(nonnegative));
hemisphere(~nonnegative) = 'S';
absvalues = num2cell(abs(dms'));
values = [absvalues; num2cell(hemisphere')];
str = sprintf('%2.0fd%2.0fm%7.5fs%s\n', values{:})

str =
    30d50m44.78012sN
    82d 2m39.90825sS
     0d30m17.12345sS
     0d 0m14.82000sN

% Split the string into cells as delimited by the newline
% character, then return to the original values using STR2ANGLE.
newline = sprintf('\n');
a = strread(str,'%s',-1,'delimiter',newline);
for k = 1:numel(a)
    str2angle(a{k})
end

ans =
          30.8457722555556

ans =
         -82.0444189583333

ans =
        -0.504756513888889

ans =
       0.00411666666666667

See Also

dms2degrees, deg2rad degrees2dm, rad2deg

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS