%% Sort by Number of Sides
% Sort the boundaries of a polygon according to their number of sides in order to access a single boundary at a time.
%
% Create a polygon containing two boundaries, one with three sides and one with four sides.

x1 = [0 1 1 0];
y1 = [0 0 1 1];
x2 = [2 3 2.5];
y2 = [2 2 3];
polyin = polyshape({x1,x2},{y1,y2});
plot(polyin)

%
% Sort the boundaries in descending order according to their number of sides, so that the four-sided boundary is indexed first in the output polyshape object.

polyout = sortboundaries(polyin,'numsides','descend');

%
% You now can reference the four-sided boundary based on its index, for example when you want to access the boundary's vertices or compute its area.

[x,y] = boundary(polyout,1)
area(polyout,1)

% Copyright 2012 The MathWorks, Inc.