Borrar filtros
Borrar filtros

how to let a "plot" immune on any later " hold off" once this "plot" is created.

24 visualizaciones (últimos 30 días)
晓通
晓通 el 7 de Abr. de 2024 a las 3:05
Comentada: 晓通 el 8 de Abr. de 2024 a las 2:21
I am a Matlab fan.
pls review my Matlab script file below:
clear;clc;
r=10;
plot([-35,35], [0,0], 'r-.' )
axis equal
hold on
for h=linspace(0,10,10)
theta=linspace(0,2*pi);
x=r*cos(theta);
y=30+h+r*sin(theta);
plot(x,y);
xlim([-50,50]);
ylim([-50,50]);
pause(0.1)
end
if I don't add " hold off" in the " for-end" statement, then the circle creates many ones when it is moving.
if I do add " hold off" in the " for-end" statement, then the horizontal red line is disappeared.
I expect :
1) the horizontal red line is kept once it is ploted before the the " for-end" statement.
2) only one circle is occuring when it is moving.
may you give me a guide?
Thanks in advance!

Respuesta aceptada

DGM
DGM el 7 de Abr. de 2024 a las 3:28
Editada: DGM el 7 de Abr. de 2024 a las 3:35
Here's one idea.
r=10;
plot([-35,35], [0,0], 'r-.' )
axis equal
hold on
% just create a dummy plot object to be used in the loop
hp = plot(0,0);
for h=linspace(0,10,10)
theta=linspace(0,2*pi);
x=r*cos(theta);
y=30+h+r*sin(theta);
% update the plot object with xy data
hp.XData = x;
hp.YData = y;
xlim([-50,50]);
ylim([-50,50]);
pause(0.1)
end
There are other ways. I'd try to pull more stuff outside the loop.
% these don't need to be inside the loop
r = 10;
h = linspace(0,10,10);
theta = linspace(0,2*pi);
plot([-35,35], [0,0], 'r-.' )
axis equal
hold on
% these probably don't need to be inside the loop either
xlim([-50,50]);
ylim([-50,50]);
for k = 1:numel(h)
x = r*cos(theta);
y = 30 + h(k) + r*sin(theta);
if k == 1
% create a new plot object on the first iteration
hp = plot(x,y);
else
% update the plot object with xy data on subsequent passes
hp.XData = x;
hp.YData = y;
end
pause(0.1)
end

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by