Documentation

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.

Examples

Convert four angles from values in degrees to degree-minutes-seconds representation.

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

The output appears as follows:

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{:})

The output appears as follows:

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');
C = textscan(str,'%s',-1,'delimiter',newline);
a = deal(C{:});
for k = 1:numel(a)
    str2angle(a{k})
end

The output appears as follows:

ans =
          30.8457722555556

ans =
         -82.0444189583333

ans =
        -0.504756513888889

ans =
       0.00411666666666667
Was this topic helpful?