MATLAB Answers

how to draw multiple patches with different number of vertices?

51 views (last 30 days)
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]};
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

Accepted Answer

Stephen on 8 Feb 2015
Edited: Stephen 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],[3],[4,5,6],[7],[8,9]};
B = cellfun('length',A);
C = cellfun(@(v,n)[v,nan(1,max(B)-n)],A,num2cell(B),'UniformOutput',false);
C = vertcat(C{:});
Stephen 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.

Sign in to comment.

More Answers (1)

Ted Shultz
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];
Ted Shultz
Ted Shultz on 2 Nov 2018
I’m certainly no expert, and patch certainly is the right tool to use most times. Polyshape is a powerful patch like object that I’m sure must have some overhead associated with it. In some situations, I’ve found that using polyshape can speed up my code (or at least speed up writing my code…). For example when I am using a “for” loop, I can generate all my Polyshape objects before displaying them (there is a significant slowdown when plotting many objects on a map). Polyhshape also has some nice methods built in that are able to replace my own much slower code (like the ability to union/subtract/xor/add/intersect polyshapes together, rotation, etc).
The code @Stephen Cobeldick included is pretty slick, and actually did answer your question, I only added my comment because I figured someone who found this old question may also want to be aware of polyshape.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by