Format distance strings


str = dist2str(distin)
str = dist2str(dist,format)
str = dist2str(dist,format,units)
str = dist2str(dist,format,digits)
str = dist2str(dist,format,units,digits)


str = dist2str(distin)converts a numerical vector of distances in kilometers, distin, to a string matrix. The output string matrix is useful for the display of distances.

str = dist2str(dist,format) uses the format string to specify the notation to be used for the string matrix. If blank or 'none', the result is a simple numerical representation (no indicator for positive distances, minus signs for negative distances). The only other format is 'pm' (for plus-minus) prefixes a + for positive distances.

str = dist2str(dist,format,units) defines the units in which the input distances are supplied, and which are encoded in the string matrix. Units must be one of the following: 'feet', 'kilometers', 'meters', 'nauticalmiles', 'statutemiles', 'degrees', or 'radians'. Note that statute miles are encoded as 'mi' in the string matrix, whereas in most Mapping Toolbox™ functions, 'mi' indicates international miles. If omitted or blank, 'kilometers' is assumed.

str = dist2str(dist,format,digits) or str = dist2str(dist,format,units,digits) uses the input digits to determine the number of decimal digits in the output matrix. digits = -2 uses accuracy in the hundredths position, digits = 0 uses accuracy in the units position. Default is digits = -2. For further discussion of specifying digits, see roundn.

The purpose of this function is to make distance-valued variables into strings suitable for map display.


Create a vector of values and convert to strings:

d = [-3.7 2.95 87];
str = dist2str(d,'none','km')

str =
-3.70 km
 2.95 km
87.00 km

Now change the units to nautical miles, add plus signs to positive values, and truncate to the tenths (10–1) slot:

str = dist2str(d,'pm','nm',-1)

str =
 -3.7 nm
 +3.0 nm
+87.0 nm

See Also


Was this topic helpful?