how to let a "plot" immune on any later " hold off" once this "plot" is created.
24 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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!
0 commentaires
Réponse acceptée
DGM
le 7 Avr 2024 à 3:28
Modifié(e) : DGM
le 7 Avr 2024 à 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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Annotations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!