How to animate contour plot in Matlab - matlab

I have 32x21 matrix with x axis increment of one parameter (2 to 64 in steps of 2) and y axis increment of n other parameter,in steps of two again. I can plot it as contour plot, but I was wondering if there is a way to have contour plot as animation/movie.Thanks

Did you try to place your plot in a for loop which iterates through all the contours? Use drawnow
e.g.
hold on
for ii = 2:2:64
%do your plotting here
%use "pause" if too fast
drawnow
end
This should update the figure every iteration.
Also see this

Related

How to update a plot with two subplot in MATLAB?

I have a function in MATLAB and it plots two curves and I run it two times.
On the first time plot main curve as you can see in the red color (first plot) and after that turn the "hold on" and execute my function again with the green color (second shape).
The problem is that the left subplot does not work and delete the first curve (red curve) but the second one works fine (last plot).
My main script is:
% some code to processing
...
roc('r.-',data); %this function plots my curves
and on the second run
% some code to processing
...
plot on
roc('g.-',data);
and my roc function contains:
%some code
...
subplot(1,2,1)
hold on
HCO1=plot(xroc(J),yroc(J),'bo');
hold off
legend([HR1,HRC1,HCO1],'ROC curve','Random classifier','Cut-off
point','Location','NorthOutside')
subplot(1,2,2)
hold on
HCO2=plot(1-xroc(J),yroc(J),'bo');
hold off
legend([HR2,HRC2,HCO2],'ROC curve','Random classifier','Cut-off
point','Location','NorthOutside')
disp(' ')
%....
Assuming your roc function calculates xroc and yroc I suggest you rewrite your code to modularize it
function [xroc,yroc] = roc(data)
%your algorithm or training code
%xroc=...
%yroc=...
end
this way your main script could be edited to something like this
%first run
[xroc1,yroc1] = roc(data);
%...some further processing with data variable
[xroc2,yroc2] = roc(data);
%plots
ax1 = subplot(1,2,1,'nextplot','add'); %create left axes
ax2 = subplot(1,2,2,'nextplot','add'); %create right axes (mirrored roc)
%now you can go ahead and make your plots
%first the not mirrored plots
plot(xroc1,yroc1,'r','parent',ax1);
plot(xroc2,yroc2,'g','parent',ax1);
%and then the mirrored plots
plot(1-xroc1,yroc1,'r','parent',ax2);
plot(1-xroc2,yroc2,'g','parent',ax2);
it is a little effort to rewrite but it surely will help to make your code scaleable to if you want to add more than just two curves in future.

Line formation in Matlab

I am a new user of not only Matlab but stackoverflow. I am looking for an information as I just started using Matlab. I went through basic commands of matlab but I am not getting an idea of how to work with below problem statement.
I want to create the below graph
There are two lines and the angle between them is variable from zero degree till 30 degree and the length of lines can be taken anything.
It will be helpful if anyone lets me know how to create the above figure in matlab.
Thanks
Since you want to plot a line, you just need to define the (x,y) coordinates of the starting and ending point of the line itself
For a given angle theta and a given starting point, you can define coordinates of the line using sin and cos functions (sind and cosd if theta is in deg):
x_start=1;
y_start=3;
x_coord=[x_start x_start+r*cosd(theta)]
y_coord=[y_start y_start+r*sind(theta(i))]
In your case, you can use a for loop to generate all the lines with theta ranging from 0 to 30°.
This is a possible implementation: at each iteration of the for loop, a line is plotted and displayed for 1 sec than it is deleted.
% Define the length of the line
r=3;
% Define the starting point coordinates
x_start=1;
y_start=3;
% Define the set of angles (in radiants)
theta=[0:30];
% Open a Figure
figure
% Add the axes
axes
daspect([1 1 1])
hold on
% Plot the reference line
x_ref=[x_start r+x_start]
y_ref=[y_start y_start]
xlim([0 r+x_start])
ylim([0 r+y_start])
plot(x_ref,y_ref,'--b','linewidth',2)
grid minor
% Plot the line
for i=1:length(theta)
% Plot the line
pl_h=plot([x_start x_start+r*cosd(theta(i))], ...
[y_start y_start+r*sind(theta(i))],'r','linewidth',2)
xlim([0 r+x_start])
ylim([0 r+y_start])
% Add a string displaying the value of the current angle
tx_h=text(1,2,['Angle= ' num2str(theta(i)) ' [Deg]'])
pause(1)
delete([tx_h pl_h])
end
Hope this helps,
Qapla'

Matlab continue graph for second loop

%free fall of a ball
clc
clear all
close all
v0=5; % initial velocity up
g=9.8; %free fall acceleration
v1=(0.7/0.9)*v0
% time of fly
tup=v0/9;
nsteps=10; %number of frames
dt=tup/nsteps; %time step
Hmax=v0*tup+(-g)*tup*tup/2; % maximum altitude
altitude(1:nsteps+1)=0; %define array for position Y
time=0:dt:tup;% define time array
%initilaise plot
figure(1)
axis([0,2*tup,0,2*Hmax]);
hold on
% loop
for i=1:nsteps
altitude(i)=v0*time(i)+(-g)*time(i)*time(i);
plot(time(i),altitude(i), 'ro')
grid on;
M(i)=getframe;
end
%loop bouncing
for i=1:nsteps
altitude(i)=v1*time(i)+(-g)*time(i)*time(i);
plot(time(i),altitude(i), 'ro')
grid on;
M(i)=getframe;
end
%make movie
movie(M);
movie2avi(M, 'C:\Users\Mehmet\Desktop\avi\mm','compression','none');
%extra plots
figure(2)
plot(time(1:nsteps),altitude(1:nsteps))
figure(3)
plot(time(1:nsteps),altitude(1:nsteps),'ro')
We have this ball bouncing simulation. What we want to do is, to continue loop 2 after loop 1 in graph.So, it will be continious bouncing simulation.2 bouncings are shown from 1:10 steps but we want second loop to be shown after 10 steps.
There are a couple of approaches, depending upon what effect you're going for. The simplest is to simply add hold on as a separate command after each of your plot commands. This will keep accumulating new lines/dots without erasing the old ones. To stop this effect, add a hold off command. The next plot command will then erase everything and plot on a clean graph.
Alternatively, if you only want to show a fixed number of the previous steps (and not all of them, as the hold on will do), you'll have to explicitly hold onto the previous values. Something like this:
% loop
max_history = 10; %plot this many symbols
for i=1:nsteps
altitude(i)=v0*time(i)+(-g)*time(i)*time(i);
%get the historical and current data points to plot
start_ind = i-max_history+1; %here's the oldest point that you'd like
start_ind = max([start_ind, 1]) %but you can't plot negative indices
inds = [start_ind:i]; %here are the ones to plot
%update the plot
plot(time(inds),altitude(inds), 'ro');
grid on;
%you might need a "drawnow" here
M(i)=getframe;
end
Copy this same idea into your other for loops and you should be good-to-go!

Matlab: replace one plot maintaining others

I have a figure in which I plot some disperse points and then a trajectory. I want to switch between different trajectories by plotting them in the same figure as the points, but without creating new figures, i.e., "erasing" the first trajectory and then plotting the new one.
Is there a way of doing this?
Perhaps this little demo will be helpful:
xy = rand(20,2);
figure
% Plot first iteration and output handles to each
h = plot(xy(:,1),xy(:,2),'b.',xy(1:2,1),xy(1:2,2),'r-');
axis([0 1 0 1])
% Update second plot by setting the XData and YData properties of the handle
for i = 2:size(xy,1)-1
set(h(2),{'XData','YData'},{xy(i:i+1,1),xy(i:i+1,2)})
drawnow
pause(0.1);
end
You should read up on handle graphics in Matlab and the get and set functions.

Matlab: Plot3 not showing the 3rd axis

All the three variables I am using to plot are matrix of size 1x1x100. I am using this code line to plot:
hold on;
for i=1:100
plot3(R_L(:,:,i),N_Pc(:,:,i),CO2_molefraction_top_of_window(:,:,i),'o');
xlabel('R_L');
ylabel('N_P_c');
zlabel('CO_2')
end
However, I am not getting the third axis, and hence the third variable CO2_molefraction_top_of_window on the plot. May I know where am I wrong?
Besides the above question, but on the same subject, I want to know if there is any option where I can plot 4 dimensional plot just like the 3 dimensional plot which can be drawn using plot3?
So I had the same problem when using plot3. For some reason, using the hold on command "flattens" the plot. I'm not sure why, but I suspect it has something to do with the operation hold on performs on the plot.
Edit: To clarify, the 3d plot is still there, but the perspective has been forced to change. If you use the "rotate 3D" tool (the one with an arrow around a cube), you can see the graph is 3d, the default perspective is just straight on so only two axes are visible and it appears flat.
Just a note --- you only need to do the xlabel ylabel zlabel commands once (outside the loop).
Also:
is there any reason your matrices are 1x1x100 instead of just 100x1 or 1x100?
Because if you reshape them to 2D you can just do the plotting in one hit.
What do you mean by "missing third axis"? When I run your code (or as close as I can get, since you didn't provide a reproducible example), I do get a 3rd axis:
.
X = rand(1,1,100); % 1x1x100 X matrix
Y = rand(1,1,100); % 1x1x100 Y matrix
Z = rand(1,1,100); % 1x1x100 Z matrix
% Now, we could do a for loop and plot X(:,:,i), Y(:,:,i), Z(:,:,i),
% OR we can just convert the matrix to a vector (since it's 1x1x100 anyway)
% and do the plotting in one go using 'squeeze' (see 'help squeeze').
% squeeze(X) converts it from 1x1x100 (3D matrix) to 100x1 (vector):
plot3(squeeze(X),squeeze(Y),squeeze(Z),'o')
xlabel('x')
ylabel('y')
zlabel('z')
This gives the following, in which you can clearly see three axes:
If it's the gridlines that you want to make the graph look "more 3D", then try grid on (which is in the examples in the Matlab help file for plot3, try help plot3 from the Matlab prompt):
grid on
You will have to clarify "missing third axis" a bit more.
I came across a similar problem and as #Drofdarb's the hold on seems to flatten out one axis. Here is a snippet of my code, hope this helps.
for iter = 1:num_iters:
% hold on;
grid on;
plot3(tita0,tita1, num_iters,'o')
title('Tita0, Tita1')
xlabel('Tita0')
ylabel('Tita1')
zlabel('Iterations')
hold on; % <---- Place here
drawnow
end
As opposed to:
for iter = 1:num_iters:
grid on;
hold on; % <---- Not here
plot3(tita0,tita1, num_iters,'o')
title('Tita0, Tita1')
xlabel('Tita0')
ylabel('Tita1')
zlabel('Iterations')
% hold on;
drawnow
end