Main Content

degrees2dms

Convert degrees to degrees-minutes-seconds

Description

DMS = degrees2dms(angleInDegrees) converts angles from values in degrees which may include a fractional part (sometimes called “decimal degrees”) to degrees-minutes-seconds representation.

example

Examples

collapse all

Convert an angle specified as a real-valued column vector to degrees-minutes-seconds representation. The output value is an n-by-3 real-valued matrix. Each row in the output specifies one angle, with the format [Degrees Minutes Seconds].

angleInDegrees = [ 30.8457722555556; ...
                  -82.0444189583333; ...
                   -0.504756513888889; ...
                    0.004116666666667];
dms = degrees2dms(angleInDegrees)
dms = 4×3

   30.0000   50.0000   44.7801
  -82.0000    2.0000   39.9082
         0  -30.0000   17.1235
         0         0   14.8200

Convert angles in degrees to degree-minute-second representation. Display the result using the default display provided by angl2str.

angleInDegrees = [ 30.8457722555556; ...
                  -82.0444189583333; ...
                   -0.504756513888889; ...
                    0.004116666666667];
angl2str(angleInDegrees,'ns','degrees2dms')    
ans = 4x25 char array
    ' 30^{\circ} 50' 44.78" N '
    ' 82^{\circ} 02' 39.91" S '
    '  0^{\circ} 30' 17.12" S '
    '  0^{\circ} 00' 14.82" N '

Alternatively, specify a custom display format by converting the angles to degree-minute-second representation and using sprintf. The result is a single string.

dms = degrees2dms(angleInDegrees)
dms = 4×3

   30.0000   50.0000   44.7801
  -82.0000    2.0000   39.9082
         0  -30.0000   17.1235
         0         0   14.8200

nonnegative = all((dms >= 0),2);
hemisphere = repmat('N', size(nonnegative));
hemisphere(~nonnegative) = 'S';
absvalues = num2cell(abs(dms'));
values = [absvalues; num2cell(hemisphere')];
sprintf('%2.0fd:%2.0fm:%7.5fs:%s\n',values{:})
ans = 
    '30d:50m:44.78012s:N
     82d: 2m:39.90825s:S
      0d:30m:17.12345s:S
      0d: 0m:14.82000s:N
     '

Input Arguments

collapse all

Angle in degrees, specified as an n-element real-valued column vector.

Output Arguments

collapse all

Angle in degrees-minutes-seconds representation, returned as an n-by-3 real-valued matrix. Each row specifies one angle, with the format [D M S]:

  • D contains the “degrees” element and is integer-valued.

  • M contains the “minutes” element and is integer-valued.

  • S contains the “seconds” element and may have a 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.

Version History

Introduced in R2007a