How can I fill multiple polygons from the same vector?

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!

 Réponse acceptée

jonas
jonas le 10 Août 2018
Modifié(e) : jonas le 21 Août 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 commentaires

hlor
hlor le 10 Août 2018
Thank you very much for your answer. And how can I automate the passage from my initial x = [] y = [], considering that x and y could have more than 3 polygons, each one with different lenght?
No problem! That depends on how your initial x/y are structured. Let's assume you separate polygons by NaNs while also starting and ending with a NaN. You could do something like this:
x = [NaN 0 1 1 0 NaN 2 3 3 2 2 NaN -1 1 1 -1 -1 NaN];
y = [NaN 0 1 2 0 NaN 3 3 4 4 3 NaN 4 4 5 5 4 NaN];
idn=find(isnan(x));
Sz=diff(idn)-1;
Nmax=max(Sz);
N=numel(Sz);
X=zeros(Nmax,N);
Y=X;
for i=1:N;
X(1:Sz(i),i)=x(idn(i)+1:idn(i+1)-1);
Y(1:Sz(i),i)=y(idn(i)+1:idn(i+1)-1);
end
patch(X,Y,[1;2;3])
Thank you Jonas. I have one more question. I need to leave empty (not white) the two triangles inside the figure, because then I have to fill them with other patches with the same vertices and different color. And I cannot set an order (one over the other), because this operation is inside a loop. Here an example:
x1 = [1 2 4 5 3 2 1 1 NaN 3 4 3 3 NaN 1 2 1 1 NaN 1 2 2 1 1];
y1 = [1 1 3 6 8 6 3 1 NaN 3 5 6 3 NaN 1 1 3 1 NaN 7 7 9 9 7];
x = [NaN x1 NaN];
y = [NaN y1 NaN];
idn=find(isnan(x));
Sz=diff(idn)-1;
Nmax=max(Sz);
N=numel(Sz);
X=zeros(Nmax,N);
Y=X;
for i=1:N;
X(1:Sz(i),i)=x(idn(i)+1:idn(i+1)-1);
Y(1:Sz(i),i)=y(idn(i)+1:idn(i+1)-1);
end
% Remove 0 at the end of each column
for j = 1:size(X,2)
for i = 2:size(X,1)
if X(i,j) == 0;
X(i,j) = X(i-1,j);
else
X(i,j) = X(i,j);
end
end
end
for j = 1:size(X,2)
for i = 2:size(X,1)
if Y(i,j) == 0;
Y(i,j) = Y(i-1,j);
else
Y(i,j) = Y(i,j);
end
end
end
figure
patch(X,Y,'r'); hold on
plot(x1,y1,'*--k')
axis([0 6 0 10])
I don't quite underestand? Like this?
x1 = [1 2 4 5 3 2 1 1 NaN 3 4 3 3 NaN 1 2 1 1 NaN 1 2 2 1 1];
y1 = [1 1 3 6 8 6 3 1 NaN 3 5 6 3 NaN 1 1 3 1 NaN 7 7 9 9 7];
x = [NaN x1 NaN];
y = [NaN y1 NaN];
idn=find(isnan(x));
Sz=diff(idn)-1;
Nmax=max(Sz);
N=numel(Sz);
X=NaN(Nmax,N);
Y=X;
for i=1:N;
X(1:Sz(i),i)=x(idn(i)+1:idn(i+1)-1);
Y(1:Sz(i),i)=y(idn(i)+1:idn(i+1)-1);
end
X = fillmissing(X,'previous')
Y = fillmissing(Y,'previous')
figure
patch(X,Y,[1,NaN,NaN,4]);
hlor
hlor le 11 Août 2018
No. I hope this is clearer.
jonas
jonas le 11 Août 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Centre d'aide et File Exchange

Question posée :

le 10 Août 2018

Modifié(e) :

le 21 Août 2018

Community Treasure Hunt

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

Start Hunting!

Translated by