How to remove some parts of a Matlab plot?

Hello,
I just wanted to know if it is possible to remove some parts of a Matlab plot. The solution is the specified region and below is the code,
clc;
clear;
close all;
w = -pi:0.01:pi;
r1 = 3;
r2 = 4;
x1 = 2.5;
y1 = -2;
x2 = 1;
y2 = 0.5;
x11 = x1+r1*cos(w);
y11 = y1+r1*sin(w);
x22 = x2+r2*cos(w);
y22 = y2+r2*sin(w);
x = -5:0.01:5;
yy1 = x-2;
yy2 = -x+2;
plot(x11,y11,'r',x22,y22,'b',x,yy1,'k',x,yy2,'k')
grid on
axis square

 Respuesta aceptada

Star Strider
Star Strider el 3 de Abr. de 2021
Try this:
w = -pi:0.01:pi;
r1 = 3;
r2 = 4;
x1 = 2.5;
y1 = -2;
x2 = 1;
y2 = 0.5;
x11 = x1+r1*cos(w);
y11 = y1+r1*sin(w);
x22 = x2+r2*cos(w);
y22 = y2+r2*sin(w);
x = -5:0.01:5;
yy1 = x-2;
yy2 = -x+2;
inred1 = inpolygon(x,yy1,x11,y11);
inred2 = inpolygon(x,yy2,x11,y11);
inblu1 = inpolygon(x,yy1,x22,y22);
inblu2 = inpolygon(x,yy2,x22,y22);
yy1(inred1 & inblu1) = NaN;
yy2(inred2 & inblu2) = NaN;
figure
plot(x11,y11,'r',x22,y22,'b',x,yy1,'k',x,yy2,'k')
grid on
axis equal
producing:
I changed the axis call to make the circles appear round.

4 comentarios

Pooya Zeg
Pooya Zeg el 3 de Abr. de 2021
Thank you, and sorry for being unclear. I just wanted the opposite. I want to keep the region that your code removed and remove the other parts.
Star Strider
Star Strider el 3 de Abr. de 2021
Editada: Star Strider el 3 de Abr. de 2021
My pleasure!
That is easy!
Just change these two assignments:
yy1(inred1 & inblu1) = NaN;
yy2(inred2 & inblu2) = NaN;
to:
yy1(~(inred1 & inblu1)) = NaN;
yy2(~(inred2 & inblu2)) = NaN;
with the rest of the code unchanged,
producing:
The code works by creating logical vectors ‘inred# and ‘inblu# (where the # is the respective number). So to do the opposite simply requires using logical negation (the ~ operator) to reverse them.
EDIT — (03 Apr 2021 at 12:27)
Equivalently:
inboth1 = inred1 & inblu1;
inboth2 = inred2 & inblu2;
figure
plot(x11,y11,'r',x22,y22,'b',x(inboth1),yy1(inboth1),'k',x(inboth2),yy2(inboth2),'k')
grid on
axis equal
producing the same result.
The rest of the code is unchanged.
.
Pooya Zeg
Pooya Zeg el 3 de Abr. de 2021
Great! Thanks. It worked. I'm just wondering if this can also work for "contour."
Star Strider
Star Strider el 3 de Abr. de 2021
As always, my pleasure!
With contour, likely not directly. It would be necessary to get the individual contours (difficult, hoiwever not impossible) and plot them separately, along with any other lines. Then a similar approach would likely work.
Note that contours, at least in my experience, do not overlap as the circles do here, so I have no idea how that would apply.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 3 de Abr. de 2021

Comentada:

el 3 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by