MATLAB: Creation of multiple rectangles

figurerectangles

Hallo,
i have got a question to my code. This function creates only 10 rectangles, so i would like to make it more efficient, that this is not only working for 10. Furthermore, i would like to change the way it is stored in the array. As you can see my array is reallly long, and only for 10 rectangles.
Any help would be nice.
x and y are both 10×1 coordinates of another figure, the rectangles are on the edge of this figure.
function S = sBox(x,y)
%sBox creates the secondary box out of coordinates
S1 = [x(1) y(1); x(2) y(1); x(2) y(2); x(1) y(2); x(1) y(1)];
S2 = [x(2) y(2); x(3) y(2); x(3) y(3); x(2) y(3); x(2) y(2)];
S3 = [x(3) y(3); x(4) y(3); x(4) y(4); x(3) y(4); x(3) y(3)];
S4 = [x(4) y(4); x(5) y(4); x(5) y(5); x(4) y(5); x(4) y(4)];
S5 = [x(5) y(5); x(6) y(5); x(6) y(6); x(5) y(6); x(5) y(5)];
S6 = [x(6) y(6); x(7) y(6); x(7) y(7); x(6) y(7); x(6) y(6)];
S7 = [x(7) y(7); x(8) y(7); x(8) y(8); x(7) y(8); x(7) y(7)];
S8 = [x(8) y(8); x(9) y(8); x(9) y(9); x(8) y(9); x(8) y(8)];
S9 = [x(9) y(9); x(10) y(9); x(10) y(10); x(9) y(10); x(9) y(9)];
S10= [x(10) y(10); x(1) y(10); x(1) y(1); x(10) y(1); x(10) y(10)];
S = [x(1) y(1)
x(2) y(1)
x(2) y(2)
x(1) y(2)
x(1) y(1)
x(2) y(2)
x(3) y(2)
x(3) y(3)
x(2) y(3)
x(2) y(2)
x(3) y(3)
x(4) y(3)
x(4) y(4)
x(3) y(4)
x(3) y(3)
x(4) y(4)
x(5) y(4)
x(5) y(5)
x(4) y(5)
x(4) y(4)
x(5) y(5)
x(6) y(5)
x(6) y(6)
x(5) y(6)
x(5) y(5)
x(6) y(6)
x(7) y(6)
x(7) y(7)
x(6) y(7)
x(6) y(6)
x(7) y(7)
x(8) y(7)
x(8) y(8)
x(7) y(8)
x(7) y(7)
x(8) y(8)
x(9) y(8)
x(9) y(9)
x(8) y(9)
x(8) y(8)
x(9) y(9)
x(10) y(9)
x(10) y(10)
x(9) y(10)
x(9) y(9)
x(10) y(10)
x(1) y(10)
x(1) y(1)
x(10) y(1)
x(10) y(10)];

Best Answer

Hi,
You can use patch function for this purpose.
Oleg in the following answer explains how that can be achieved.
Hope this helps!