MATLAB: plot in a loop - matlab

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.

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

Image plot only showing after last loop iteration [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

Clearing isosurface (or other plots) from an axes

I use the following command:
isosurface(data,color)
Now if I use the same command with different data again, it gets superimposed on the previous one.
So, I tried doing:
p = patch(isosurface(foo));
isonormals(foo,p)
delete(p);
to delete the previous plot, but this way i can't use the colorbar.
How do I go about this?
Have you tried this?
cla(gca)
cla clears an axes and gca is the handle to the current axes you're are plotting on.
Assuming you just want another patch plot in the same figure window, just use
hold off

MATLAB - display figure only when everything has been plotted

I'm trying to plot a 2d finite element solution and am plotting triangle-by-triangle like so:
for i=1:K
figure(1)
fill3(x,y,z,c)
hold on
end
The problem with this is that when I run the code, it literally draws them all in real time so I can see each triangle being drawn. What I want it to just have the finished figure pop up once it's all done.
My friend has coded the same thing except she's not having this issue at all and we can't find any differences in the code. My computer is very fast so it's not an issue of lag. I'm thinking maybe there's a setting in MATLAB that I'm missing?
Edit: I found the problem. Apparently, putting 'figure(1)' inside the loop makes a huge difference. I timed it with 'tic' and 'toc' and it took 54 seconds with the 'figure(1)' label inside the loop and 2 seconds when it was moved just before the loop. Go figure...
When you start the figure, set the visible property to 'off'; when you are done plotting, set the visibility to 'on'.
h = figure('visible','off');
hold on;
for i=1:K
fill3(x,y,z,c);
end
hold off;
set(h,'visible','on');
Also, I'm not entirely sure how the event queue works in MATLAB, but I'm fairly sure the fact you have a fast computer is effecting this. You can simulate what's happening with your computer by using drawnow on your friends slower comp within the for-loop.

How to create a new figure in MATLAB?

Usually when I plot in MATLAB, it always draws on the same figure. How do I make it draw in a new figure?
I know it is pretty elementary, but I'm not finding it using Google Search.
figure;
plot(something);
or
figure(2);
plot(something);
...
figure(3);
plot(something else);
...
etc.
While doing "figure(1), figure(2),..." will solve the problem in most cases, it will not solve them in all cases. Suppose you have a bunch of MATLAB figures on your desktop and how many you have open varies from time to time before you run your code. Using the answers provided, you will overwrite these figures, which you may not want. The easy workaround is to just use the command "figure" before you plot.
Example: you have five figures on your desktop from a previous script you ran and you use
figure(1);
plot(...)
figure(2);
plot(...)
You just plotted over the figures on your desktop. However the code
figure;
plot(...)
figure;
plot(...)
just created figures 6 and 7 with your desired plots and left your previous plots 1-5 alone.
The other thing to be careful about, is to use the clf (clear figure) command when you are starting a fresh plot. Otherwise you may be plotting on a pre-existing figure (not possible with the figure command by itself, but if you do figure(2) there may already be a figure #2), with more than one axis, or an axis that is placed kinda funny. Use clf to ensure that you're starting from scratch:
figure(N);
clf;
plot(something);
...
As has already been said: figure will create a new figure for your next plots. While calling figure you can also configure it. Example:
figHandle = figure('Name', 'Name of Figure', 'OuterPosition',[1, 1, scrsz(3), scrsz(4)]);
The example sets the name for the window and the outer size of it in relation to the used screen.
Here figHandle is the handle to the resulting figure and can be used later to change appearance and content. Examples:
Dot notation:
figHandle.PaperOrientation = 'portrait';
figHandle.PaperUnits = 'centimeters';
Old Style:
set(figHandle, 'PaperOrientation', 'portrait', 'PaperUnits', 'centimeters');
Using the handle with dot notation or set, options for printing are configured here.
By keeping the handles for the figures with distinc names you can interact with multiple active figures. To set a existing figure as your active, call figure(figHandle). New plots will go there now.
Another common option is when you do want multiple plots in a single window
f = figure;
hold on
plot(x1,y1)
plot(x2,y2)
...
plots multiple data sets on the same (new) figure.
As simple as this-
figure, plot(yourfigure);