How can I fill multiple polygons from the same vector?

5 Ansichten (letzte 30 Tage)
hlor
hlor am 10 Aug. 2018
Bearbeitet: jonas am 21 Aug. 2018
Hi everyone! I have a problem that seems to be trivial, but that I cannot solve. I have two vectors x=[...] and y=[...]. These two vectors contain the coordinates of three polygons, divided by NaN. I would like to fill these three areas without splitting the vectors (two red squares and one red triangle). Here is the code
x = [0 1 1 0 NaN 2 3 3 2 2 NaN -1 1 1 -1 -1];
y = [0 1 2 0 NaN 3 3 4 4 3 NaN 4 4 5 5 4];
figure
plot(x,y,'k'); hold on
fill(x,y,'r','LineStyle','none')
I also tried to remove NaN, without success.
x1 = [0 1 1 0 2 3 3 2 2 -1 1 1 -1 -1];
y1 = [0 1 2 0 3 3 4 4 3 4 4 5 5 4];
figure
fill(x1,y1,'r','LineStyle','none')
Any suggestions? Thank you!

Akzeptierte Antwort

jonas
jonas am 10 Aug. 2018
Bearbeitet: jonas am 21 Aug. 2018
Use patch. Each patch is defined by the coordinates of the columns of the input matrix. As such, all patches must have the same number of coordinates. You can pad the shorter patches by repeating the last coordinate.
x = [0 1 1 0 0, 2 3 3 2 2,-1 1 1 -1 -1];
y = [0 1 2 0 0, 3 3 4 4 3, 4 4 5 5 4];
x=reshape(x,5,3)
y=reshape(y,5,3)
h=patch(x,y,[1;0;2]);
  6 Kommentare
hlor
hlor am 11 Aug. 2018
No. I hope this is clearer.
jonas
jonas am 11 Aug. 2018
Oki. Then you have to either make polygons with holes (see e.g. polyshape ) or fill by white. I don't understand why you do not want to fill them by white. You can adjust the color and/or vertices after you create the object, if you need to.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by