Image plot only showing after last loop iteration [duplicate] - matlab

I am doing something like this:
a = [1:100];
for i=1:100,
plot([1:i], a(1:i));
end
My issue is that the plot is not shown until the loop is finished.
How can I show/update the plot in every iteration?

Use DRAWNOW
a = [1:100];
for i=1:100,
plot([1:i], a(1:i));
drawnow
end
Alternatively, you may want to have a look at ANYMATE from the file exchange.

Another way to do this if you just want to visualise it without saving the animation, is to use refreshdata instead of plot for subsequent plots. You will still need to call drawnow for it to update on-screen.
either use
set(fig_handle,'XData',new_xdata_array)
set(fig_handle,'YData',new_ydata_array)
refreshdata
drawnow
or use
set(fig_handle,'XDataSource',xdata_array)
set(fig_handle,'YDataSource',ydata_array)
%call this whenever xdata_array and ydata_array are assigned new values to see it updated in the plot
refreshdata
drawnow
for your example, this might look like:
a=[1:100];
figure;
h=plot(1,a(1));
for i=2:100
set(h,'XData',[1:i])
set(h,'YData',a(1:i))
refreshdata
drawnow
end
It's not all that useful for simple line plots (for which plot(); drawnow; is simpler and faster), but when you need to create more complicated figures involving multiple plot types, this can be useful.

From the documentation for comet.m
t = 0:.01:2*pi;
x = cos(2*t).*(cos(t).^2);
y = sin(2*t).*(sin(t).^2);
comet(x,y);

Matlab allows you to sort-of automate a loop statement for variables
x = 0.0:0.1:2*pi
plot(x,cos(x));
is an example......
A lot of times you don't really need to plot 'in' a loop

Related

Dynamic Plotting in a nested loop in Matlab [duplicate]

I am doing something like this:
a = [1:100];
for i=1:100,
plot([1:i], a(1:i));
end
My issue is that the plot is not shown until the loop is finished.
How can I show/update the plot in every iteration?
Use DRAWNOW
a = [1:100];
for i=1:100,
plot([1:i], a(1:i));
drawnow
end
Alternatively, you may want to have a look at ANYMATE from the file exchange.
Another way to do this if you just want to visualise it without saving the animation, is to use refreshdata instead of plot for subsequent plots. You will still need to call drawnow for it to update on-screen.
either use
set(fig_handle,'XData',new_xdata_array)
set(fig_handle,'YData',new_ydata_array)
refreshdata
drawnow
or use
set(fig_handle,'XDataSource',xdata_array)
set(fig_handle,'YDataSource',ydata_array)
%call this whenever xdata_array and ydata_array are assigned new values to see it updated in the plot
refreshdata
drawnow
for your example, this might look like:
a=[1:100];
figure;
h=plot(1,a(1));
for i=2:100
set(h,'XData',[1:i])
set(h,'YData',a(1:i))
refreshdata
drawnow
end
It's not all that useful for simple line plots (for which plot(); drawnow; is simpler and faster), but when you need to create more complicated figures involving multiple plot types, this can be useful.
From the documentation for comet.m
t = 0:.01:2*pi;
x = cos(2*t).*(cos(t).^2);
y = sin(2*t).*(sin(t).^2);
comet(x,y);
Matlab allows you to sort-of automate a loop statement for variables
x = 0.0:0.1:2*pi
plot(x,cos(x));
is an example......
A lot of times you don't really need to plot 'in' a loop

MATLAB's drawnow doesn't flush

Is there any reason that MATLAB's drawnow wouldn't flush?
This is my code:
j=1;
for k = 1:length(P)
for i = 1:n
plot(P(k,j),P(k,j+1),'.');
j = j+2;
end
axis equal
axis([-L L -L L]);
j=1;
drawnow
end
(rungekutta4 is my own function I wrote, and it works OK, so the problem isn't there.)
The particles just stay drawn on the plot and don't get overwritten every time the loop executes.
How could I fix this problem?
The proper and efficient way to do this is with handle graphics. You should also vectorize your plot commands.
% Example data to make runnable
L = 1;
n = 10; % Number of points
P = 2*rand(1e2,n+1)-1;
% Initialize plot, first iteration
h = plot(P(1,1:n),P(1,2:n+1),'.'); % Plot first set of points and return handle
axis equal;
axis([-L L -L L]);
hold on; % Ensure axis properties are fixed
drawnow;
% Animate
for k = 2:size(P,1) % size is safer in this case
% Use handle to update the positions of the previously plotted points
set(h,{'XData','YData'},{P(k,1:n),P(k,2:n+1)});
drawnow;
pause(0.1); % Slow down animation a bit to make visible
end
Calling clf and/or plot on each iteration of an animation causes many things already in memory to be unnecessarily deleted and reallocated, resulting much slower code. It may also result in flickering in some cases.
See also this very similar question and answer.
When you want to animate something, you want to, of course, force draw. Thats what the command drawnow is there for. But there are other things to consider!
One of them is that you need to make sure that everything is drawn in each frame. For that, use the hold on function just before you start drawing (plot).
However, you also need to make sure that you clear the image before drawing, else the plots will stack forever. Use the clf "clear figurecommand before the previously mentionedhold on` and that will do the job.
remember that if the animation is too fast you can always add a pause(0.2) line after drawnow to slow it.

two simultaneous plots in matlab

I want to plot two simultaneous plots in two different positions in Matlab, looped animations and both are different animations, one with hold on and another with hold off.
Also, one is 2D and one is 3D
I am doing something like this:
for i=1:some_number
axes('position',...)
plot(...);hold on;
axes('position',...)
clf
plot3(...) (or fill3 but has to do with 3d rendering)
view(...)
set(gca, 'cameraview',...)
set(gca,'projection',...)
mov(i)=getframe(gcf)
end
Q1. Do the set properties effect first axes? if so, how to avoid that?
Q2. In my plot hold on did not work. Both were instantenous. like using hold off. How do I make it work?
Q3. I hope the mov records both axes.
P.S. I hope clf is not a problem. I must use clf or if there are equivalents more suitable in my case do suggest me.
You need to store the return from the axes function and operate specifically on a given axes with subsequent function calls, rather than just the current axes.
% Create axes outside the loop
ax1 = axes('position',...);
ax2 = axes('position',...);
hold(ax1, 'on');
for i=1:some_number
plot(ax1, ...);
cla(ax2); % use cla to clear specific axes inside the loop
plot3(ax2, ...) (or fill3 but has to do with 3d rendering)
view(ax2, ...)
set(ax2, 'cameraview',...)
set(ax2,'projection',...)
mov(i)=getframe(gcf)
end
Here is a snippet from a piece of my code which plots orbits of three celestial bodies which I think will help you:
for i = 1:j, %j is an arbitrary number input by the user
plot(x, y, '*')
plot(x2, y2, 'r')
plot(xa, ya, '+')
grid on
drawnow %drawnow immediately plots the point(s)
hold on %hold on keeps the current plot for future plot additions
%dostuff to x,y,x2,y2,xa,ya
end
The two main functions you want are the drawnow and hold on.
Just to note: x,y,x2,y2,xa, and ya change with each iteration of the loop, I have just omitted that code.
EDIT: I believe the drawnow function will solve your problem with hold on.
I think this may solve your problem.
for i=1:some_number
axes('position',...)
plot(...);
drawnow %also note that you must not put the ; at the end
hold on %see above comment
axes('position',...)
clf
plot3(...) (or fill3 but has to do with 3d rendering)
view(...)
set(gca, 'cameraview',...)
set(gca,'projection',...)
mov(i)=getframe(gcf)
end

MATLAB: plot in a loop

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
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
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
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.
you can also use figure(t) to have 5 different figures.
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.

Plot inside a loop in MATLAB

I am doing something like this:
a = [1:100];
for i=1:100,
plot([1:i], a(1:i));
end
My issue is that the plot is not shown until the loop is finished.
How can I show/update the plot in every iteration?
Use DRAWNOW
a = [1:100];
for i=1:100,
plot([1:i], a(1:i));
drawnow
end
Alternatively, you may want to have a look at ANYMATE from the file exchange.
Another way to do this if you just want to visualise it without saving the animation, is to use refreshdata instead of plot for subsequent plots. You will still need to call drawnow for it to update on-screen.
either use
set(fig_handle,'XData',new_xdata_array)
set(fig_handle,'YData',new_ydata_array)
refreshdata
drawnow
or use
set(fig_handle,'XDataSource',xdata_array)
set(fig_handle,'YDataSource',ydata_array)
%call this whenever xdata_array and ydata_array are assigned new values to see it updated in the plot
refreshdata
drawnow
for your example, this might look like:
a=[1:100];
figure;
h=plot(1,a(1));
for i=2:100
set(h,'XData',[1:i])
set(h,'YData',a(1:i))
refreshdata
drawnow
end
It's not all that useful for simple line plots (for which plot(); drawnow; is simpler and faster), but when you need to create more complicated figures involving multiple plot types, this can be useful.
From the documentation for comet.m
t = 0:.01:2*pi;
x = cos(2*t).*(cos(t).^2);
y = sin(2*t).*(sin(t).^2);
comet(x,y);
Matlab allows you to sort-of automate a loop statement for variables
x = 0.0:0.1:2*pi
plot(x,cos(x));
is an example......
A lot of times you don't really need to plot 'in' a loop