Documentation Center

  • Trial Software
  • Product Updates


Merge line segments with matching endpoints


[latMerged, lonMerged] = polymerge(lat, lon)
[latMerged, lonMerged] = polymerge(lat, lon, tol)
[latMerged, lonMerged] = polymerge(lat, lon, tol, outputFormat)


[latMerged, lonMerged] = polymerge(lat, lon) accepts a multipart line in latitude-longitude with vertices stored in arrays lat and lon, and merges the parts wherever a pair of end points coincide. For this purpose, an end point can be either the first or last vertex in a given part. When a pair of parts are merged, they are combined into a single part and the duplicate common vertex is removed. If two first vertices coincide or two last vertices coincide, then the vertex order of one of the parts will be reversed. A merge is applied anywhere that the end points of exactly two distinct parts coincide, so that an indefinite number of parts can be chained together in a single call to polymerge. If three or more distinct parts share a common end point, however, the choice of which parts to merge is ambiguous and therefore none of the corresponding parts are connected at that common point.

The inputs lat and lon can be column or row vectors with NaN-separated parts (and identical NaN locations in each array), or they can be cell arrays with each part in a separate cell. The form of the output arrays, latMerged and lonMerged, matches the inputs in this regard.

[latMerged, lonMerged] = polymerge(lat, lon, tol) combines line segments whose endpoints are separated by less than the circular tolerance, tol. tol has the same units as the polygon input.

[latMerged, lonMerged] = polymerge(lat, lon, tol, outputFormat) allows you to request either the NaN-separated vector form for the output (set outputFormat to 'vector'), or the cell array form (set outputFormat to 'cell').


lat = [1 2 3 NaN 6 7 8 9 NaN 6 5 4 3 NaN 12 13 14 ...
   NaN 9 10 11 12]';
lon = lat;
[lat2, lon2] = polymerge(lat, lon);
[lat2, lon2]

ans =

     1     1
     2     2
     3     3
     4     4
     5     5
     6     6
     7     7
     8     8
     9     9
    10    10
    11    11
    12    12
    13    13
    14    14
   NaN   NaN

See Also


Was this topic helpful?