1

I've tried to do plot inside of a loop and it prints only the last plot.

How can i fix it?

I've tried to use hold on and drawnow after the plot definition but it didn't work.

This is my code:

for t=1:5
  alive = Game(World , Generations, speed);
  plot(hplot2,1:Generations,alive);
end

5 Answers 5

2

hold on should work. Try this:

figure
hplot2=gca;
hold on
for t=1:5
    alive = rand(1,Generations);
    plot(hplot2,1:Generations,alive);
end
Sign up to request clarification or add additional context in comments.

Comments

2

Sticking in a "figure" has always worked for me.

for t=1:5
    alive = Game(World , Generations, speed);
    figure;
    plot(hplot2,1:Generations,alive);
end

Comments

1

Since you are already passing the axes handle to plot, you only need to put something like pause(0.1) inside the loop, and your original source will work.

Comments

1

you can also use figure(t) to have 5 different figures.

Comments

0

If the function Game(World , Generations, speed) is a deterministic function - it gives the same output for every t. Therefore, every plot commands has exactly the same output and you cannot distinguish the first from the last plot.

Try plot a random series at each iteration (as in answer of shoelzer) and see if you see all 5 plots.

Additionally, you might want to use hold all instead of hold on: this way each plot will get a different color from the colormap.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.