MATLAB: How to make only the second plot changing in a loop

plotsecond plot changing in a looptwo plots

I'm doing something like this:
imagesc(pher_mat)
hold on
for i=1:5000
figure(1)
plot(x,y)
end
But this is adding more and more plots to the figure and I only want the first imagesc(pher_mat) and the last of each loop to be plotted. And I don't want imagesc(pher_mat) inside the loop of course.
Thanks

Best Answer

imagesc(pher_mat)
hold on
% figure(1) % figure won't change unless somewhere else there's another referenced
for i=1:5000
... % whatever changes x,y here during the loop
end
plot(x,y) % plot the last when the loop's done...
Leaves you with the visual as described -- now it the loop is very time-consuming, nothing shows until it's done, but if you don't want the intermediates to show, then don't plot 'em...
OTOH, you could "have your plot and eat it too", if you simply updated the [X|Y]Data property each pass--
imagesc(pher_mat)
hold on
i=1;
% do whatever needed for the first case to get x,y
hL=plot(x,y) % create the first plot, save line handle
for i=2:5000 % now the rest in loop
... % whatever changes x,y here during the loop
set(hL,'XData',x,'YData',y) % and update each pass
end
You can also do things like compute mod(i,10), say, and do every 10th or whatever...
"Salt to suit!" :)