Main Content


Close all rings in multipart polygon


[xdata, ydata] = closePolygonParts(xdata, ydata)
[lat, lon] = closePolygonParts(lat, lon, angleunits)


[xdata, ydata] = closePolygonParts(xdata, ydata) ensures that each ring in a multipart (NaN-separated) polygon is “closed” by repeating the start point at the end of each ring, unless the start and end points are already identical. Coordinate vectors xdata and ydata must match in size and have identical NaN locations.

[lat, lon] = closePolygonParts(lat, lon, angleunits) works with latitude-longitude data and accounts for longitude wrapping with a period of 360 if angleunits is 'degrees' and 2*pi if angleunits is 'radians'. For a ring to be considered closed, the latitudes of its first and last vertices must match exactly, but their longitudes need only match modulo the appropriate period. Such rings are returned unaltered.


collapse all

Create two vectors of planar coordinates.

xOpen = [1 0 2 NaN 0.5 0.5 1 1];
yOpen = [0 1 2 NaN 0.8 1 1 0.8];

Create a closed polygon from these coordinates.

[xClosed, yClosed] = closePolygonParts(xOpen,yOpen)
xClosed = 1×10

    1.0000         0    2.0000    1.0000       NaN    0.5000    0.5000    1.0000    1.0000    0.5000

yClosed = 1×10

         0    1.0000    2.0000         0       NaN    0.8000    1.0000    1.0000    0.8000    0.8000

Display all variables.

  Name         Size            Bytes  Class     Attributes

  xClosed      1x10               80  double              
  xOpen        1x8                64  double              
  yClosed      1x10               80  double              
  yOpen        1x8                64  double              

Load coastline data from MAT-file.

load coastlines

Construct a two-part polygon based on the coastlines data. The first ring is Antarctica. The longitude of its first vertex is -180 and the longitude of its last vertex is 180. The second ring is a small island from which the last vertex, a replica of the first vertex, is removed.

[latparts, lonparts] = polysplit(coastlat, coastlon);
latparts{2}(end) = [];
lonparts{2}(end) = [];
latparts(3:end) = [];
lonparts(3:end) = [];
[lat, lon] = polyjoin(latparts, lonparts);

Examine how closePolygonParts treats the two rings. In both cases, the first and last vertices differ. However, Antarctica remains unchanged while the small island is closed back up.

[latClosed, lonClosed] = closePolygonParts(lat, lon, 'degrees');
[latpartsClosed, lonpartsClosed] = polysplit(latClosed, lonClosed);
lonpartsClosed{1}(end) - lonpartsClosed{1}(1)  % Result is 360
ans = 360
lonpartsClosed{2}(end) - lonpartsClosed{2}(1)  % Result is 0
ans = 0

Version History

Introduced in R2006a