Problem with hold on plot

Hi, im working with some data, specifically the particles in a nozzle.
The point is that I want to create an animation using plot and for. To do this, I made a sketch of the nozzle using simple lines with plot, then for takes care of showing particles.
The problem I have is that because I need to show the sketch and the particles, I use hold on, which generates a repetition in the data of particles, which I dont want. How can I solve this problem? Be able to graph the particles and the sketch in the animation without repeating particle data.
Here is the sketch:
bosquejo.jpg
And that's how it shouldn't look:
no.jpg
And this is how it should look, but with the background sketch:
si.JPG
Animation code, just in case:
figure
hold on
title(['Velocidad en alimentador ' titulo])
for time=1:20:size(Xt,2);
pointsize=5;
plot([180e-3,180e-3],[-10e-3,10e-3],'LineWidth',4)
plot([0,20]*1e-3,[17.5,17.5]*1e-3,'k',[20,40]*1e-3,[17.5,1]*1e-3,'k',[40,170]*1e-3,[1,8]*1e-3,'k',[170,170]*1e-3,[8,-8]*1e-3,'k',[170,40]*1e-3,[-8,-1]*1e-3,'k',[40,20]*1e-3,[-1,-17.5]*1e-3,'k',[20,0]*1e-3,[-17.5,-17.5]*1e-3,'k',[0,0]*1e-3,[-17.5,17.5]*1e-3,'k')
scatter(Xt(:,time),Yt(:,time), pointsize, Vt(:,time),'filled')
ylim([-18e-3,18e-3])
xlim([0e-3,180e-3])
pause(0.001)
end
I hope the question is understood.
Greeting and thank you very much in advance!

 Risposta accettata

Adam Danz
Adam Danz il 3 Feb 2020
The loop should only contain the data you're adding to the plot.
Try this out.
figure
axes()
hold on
ylim([-18e-3,18e-3])
xlim([0e-3,180e-3])
title(['Velocidad en alimentador ' titulo])
pointsize=5;
plot([180e-3,180e-3],[-10e-3,10e-3],'LineWidth',4)
plot([0,20]*1e-3,[17.5,17.5]*1e-3,'k',[20,40]*1e-3,[17.5,1]*1e-3,'k',[40,170]*1e-3,[1,8]*1e-3,'k',[170,170]*1e-3,[8,-8]*1e-3,'k',[170,40]*1e-3,[-8,-1]*1e-3,'k',[40,20]*1e-3,[-1,-17.5]*1e-3,'k',[20,0]*1e-3,[-17.5,-17.5]*1e-3,'k',[0,0]*1e-3,[-17.5,17.5]*1e-3,'k')
for time=1:20:size(Xt,2);
scatter(Xt(:,time),Yt(:,time), pointsize, Vt(:,time),'filled')
pause(0.001)
end

9 Commenti

Hi, thanks for your answer!
As you can see, doesn't work, because the data from scatter is repeated, I need that in every time from for the scatter show only once.
no2.JPG
For example, when the loop is finishing, it should look like this (but with the sketch)
si2.JPG
Adam Danz
Adam Danz il 4 Feb 2020
"As you can see, doesn't work, "
Actually, I can't see that it doesn't work because 1) I can't run your code and more importantly, 2) it's not clear what you're trying to do. But I do know that the lines I removed from the for-loop shouldn't have been within the for-loop.
In the line below, time specifies the column of data being plotted. If these variables are matrices, that will result in several points added each iteration.
scatter(Xt(:,time),Yt(:,time), pointsize, Vt(:,time),'filled')
I don't know what it means "the data from scatter is repeated".
Cristián Vera
Cristián Vera il 4 Feb 2020
Modificato: Cristián Vera il 4 Feb 2020
"As you can see, doesn't work, "
I said that for the image that came next
Sorry for not beeing clear, the data from each array is--> Xt(particle,time), where Xt is the x position, Yt the y position and Vt the velocity. So.. when I use for related with time it's showing -in the animation- all the particles for every single time.
The problem with your proposal is that because we are using hold on function, the for function is repeating and accumulating the data in the animation for every time (from scatter). I need that this data shows just once for every loop. That's my problem. Hope to be clearer this time.
Im going to add a link from cloud service to share you what I need in the animation, and what is happening.
I need this with the sketch: Video 1
And this happen with your proposal and the problem I'm having: Video 2
Thanks again!
EDIT:
it's the other way around
I need this with the sketch: Video 2
And this happen with your proposal and the problem I'm having:Video 1
Adam Danz
Adam Danz il 4 Feb 2020
In video2, data that is plotted is disappearing and I don't see how that would happen with any of the for-loops in this thread. Can you attach a mat file with all of the variables needed to run the code?
Cristián Vera
Cristián Vera il 4 Feb 2020
Modificato: Cristián Vera il 4 Feb 2020
I made a mistake with videos. Sorry!
it's the other way around
I need this with the sketch: Video 2
And this happen with your proposal and the problem I'm having:Video 1
Give some minutes and I will upload .mat file
Here if the .mat file
Adam Danz
Adam Danz il 4 Feb 2020
Modificato: Adam Danz il 4 Feb 2020
Now that makes more sense. Data animation could take both forms which is why the goal wasn't clear.
Before I try it with you data, try this. Notice I also replaced pause() with drawnow(). See drawnow limitrate to control how frequent the plot is updated.
sh = gobjects(1);
for time=1:20:size(Xt,2);
delete(sh)
sh = scatter(Xt(:,time),Yt(:,time), pointsize, Vt(:,time),'filled');
drawnow % or pause(0.001)
end
It worked!!! Thank you for your time and kindly help!
What a relief... =)
Adam Danz
Adam Danz il 4 Feb 2020
Ha! Glad I could help.

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by