# how to draw multiple patches with different number of vertices?

71 views (last 30 days)
Itzik Ben Shabat on 8 Feb 2015
Commented: Ted Shultz on 2 Nov 2018
Hi, I am trying to draw many patches which have a different number of vertices. I have the vertices stored in the matrix V with size(MX3), m is the number of points. and I have the faces vertex indexes in a cell array of size (NX1), N is the number of faces. in each cell of C there is a different number of indexes for a given face (3 vertices indexes for a triangle, 4 for a quad etc). my question is: is there a better way to draw all of these patches without using a for loop? i tried using patch(FV) but t doesnt work for cell arrays. so for example:
V=[0 0;0 1;1 0;1 1;2 0];
C={[1 2 3];[3 2 4 5]};
FV.vertices=V;
FV.faces=C{1};
patch(FV);
FV.faces=C{2};
patch(FV,'facecolor','blue');
this will draw the first polygon black and the second blue but the question is how can i draw both of them at once? (in the real case i have a much larger number of polygons); thanks

Stephen Cobeldick on 8 Feb 2015
Edited: Stephen Cobeldick on 8 Feb 2015
Yes, you can plot them all at once by padding your matrix with NaN's.
The patch documentation states MATLAB does not require each face to have the same number of vertices. In cases where they do not, pad the end of the Faces matrix with NaNs. To define a patch with faces that do not close, add one or more NaNs to the row in the Vertices matrix that defines the vertex you do not want connected.
If you have just a few vectors, then you can create this matrix by hand.
If you need to combine many such vectors together, padding them with NaN's, you should be able to adapt this code to concatenate unequal-length vectors together into one matrix, padding with NaN's:
A = {[0,1,2],,[4,5,6],,[8,9]};
B = cellfun('length',A);
C = cellfun(@(v,n)[v,nan(1,max(B)-n)],A,num2cell(B),'UniformOutput',false);
C = vertcat(C{:});

Show 1 older comment
Itzik Ben Shabat on 8 Feb 2015
Thank you !! ! !!! :)
Dave on 26 Feb 2015
I got past this part, and was able to pad out my matrix, but now the first set of polygons won't display any color.
I tried making my polygons with multiple separate calls to 'patch', but the axes' color limit scale would not readjust with each one (i.e., even if polygon 2 was higher value than polygon 1, both would be displayed as the darkest shade on the scale)
Stephen Cobeldick on 26 Feb 2015
@Dave: Interesting problem. I think you should submit this as a new question, and please give the exact values that you are using to create the patches with.

Ted Shultz on 1 Nov 2018
Sometimes when I run into this class of problem, I find that the polyshape tool is the answer. For example, if I want to plot two triangles and a square, all at once (and the same color/properties) I can use:
% shapes are in columns
x = [
2 5 3
2 5 4
8 8 4
nan nan 3];
y = [
4 0 1
8 2 1
4 0 2
nan nan 2];
figure
ps=polyshape(x(:),y(:));
h=plot(ps);