Animated plot in matlab - matlab

I am trying to create an animated plot of triangles and the end result should be ten triangles followed by two bigger triangles followed by a straight line. Using the matlab documentation, I ended up having this, which results an animated sin plot:
h = animatedline;
axis([0 4*pi -1 1])
x = linspace(0,4*pi,2000);
for k = 1:length(x)
y = sin(x(k));
addpoints(h,x(k),y);
drawnow
end
The problem is that the plot is really slow and as soon as I changed y=sin(x(k)) to a triangular form, it got even worst. Is there a better way to do an animated plot or at least to adjust the speed? (if the speed is not dependant on the computer)

You can speed it up a little by
Computing a y vector at once, instead of computing each value in the loop.
Updating the XData and YData properties of a plot, instead of using animatedline.
The code becomes:
h = plot(NaN,NaN);
axis([0 4*pi -1 1])
x = linspace(0,4*pi,2000);
y = sin(x);
for k = 1:length(x)
set(h, 'XData', x(1:k), 'YData', y(1:k))
drawnow
end
The gain in speed is small, though. If you need more speed you probably need to decrease the number of points.

You can examine comet function to animate the curve:
t = linspace(0,4*pi,2000);
comet(t, sin(t));
It would be smooth and easier to animate a curve (see its documentation).
Also, for 3d curves you can use comet3.

Related

MATLAB: function point_cloud

Question:
Write a function called point_cloud that takes one scalar as an input argument (the function does not have to check the format of the input) and has no output argument.
If it is called like this, point_cloud(100), then it plots 100 points. Each point has a random x coordinate and a random y coordinate, each of which is gotten by a call to randn, which uses a normal distribution with a standard deviation equal to 1. The range of the plot axes should be −5 to 5 in both the x and y dimensions. The grid should be turned off. The points should be plotted and displayed one at a time by calling plot with only one point specified and, following the call of plot, by a call of drawnow, which causes the point to be plotted immediately. The command hold on should be included so that all previous points are retained when a new point is plotted.
Figure 2.41 shows an example view of the plot after point_cloud(100000) has completed its point-by-point plotting on a Mac. (Note that on Windows the points are much larger. Also note that it takes a long time to plot this many points with drawnow. Finally, try zooming in the middle.)
Figure 2.41
My Code:
function point_cloud(N)
hold on
grid off
axis([-5,5,-5,5])
for ii = 1:N
plot(randn(ii));
drawnow;
end
I know this is wrong, but I'm not sure how to solve this problem. Can someone help?
Solved code:
function point_cloud(N)
figure
hold on
grid off
axis([-5,5,-5,5])
x = randn(N,1);
y = randn(N,1);
for ii = 1:N
plot(x(ii),y(ii),'b.');
drawnow;
end
You do not need the for loop at all. And drawing the plot each iteration is very time consuming. How about rather using the scatter function.
figure
hold on
grid off
axis([-5,5,-5,5])
x = randn(N,1);
y = randn(N,1);
scatter(x,y,'b.')
This will be a lot faster.
To add to the other answer, here is the code as a function, with the added functionality that the points are one pixel on Windows as well:
function point_cloud(N)
f = figure;
x = randn(N,1);
y = randn(N,1);
scatter(x,y,1/36,'b.');
f.GraphicsSmoothing = 'off';
grid off
axis([-5,5,-5,5])
axis equal
end
The size of the markers is set with the third parameter of scatter: 1/36. The graphics smoothing of the figure needs to be set to 'off' as well, to make sure that the pixels don't become blurry or lighter.
Here's a 3D version:
function point_cloud3D(N)
f = figure;
x = randn(N,1);
y = randn(N,1);
z = randn(N,1);
scatter3(x,y,z,1/36,'b.');
f.GraphicsSmoothing = 'off';
grid off
axis([-5,5,-5,5,-5,5])
axis square
view(3)
end

Rotating a 3D figure in MATLAB

I generated a 3D surface in matlab.
clear all;
close all;
clc;
x = [1:0.1:5];
y=[1:50];
[n1 n2] = size(x);
[m1, m2] = size(y);
for i = 1 : m2
for j = 1 : n2
z(i,j) = (x(1,j)) / (y(1,i));
end
end
[x, y] = meshgrid(x, y);
surf(y, x, z)
colorbar
xlabel('x')
ylabel('y')
zlabel('z')
I got the following plotted surface:
I am interested now to rotate the cube of a quarter turn in the clockwise direction. I know that I can use the "rotate3d on" and choose the best Az and EI, but I didn't understand what are Az and EI and how should be equal to respond to my need?
For example:
There also another function called camroll(). But I don't know what must the value in () in order to rotate the cube of a quarter turn in the clockwise direction. Does 90 degree is the correct answer?
Any help will be very appreciated!
To rotate the figure axis you can use the view command:
surf(rand(20))
for az = 360:-1:-0
view(az, 30)
pause(0.01)
end
Azimus has values in between 0° and 360°. To turn the axis figure by 90° in counter-clockwise direction you oculd choose the azimuths of: [270, 180, 90, 0]
as #ASantosRibeiro mentioned, the rotate function will produce the output you want. As an aside, here is a vectorized version of your code, which you might find useful if you have a significantly larger data set.
x = 1:0.1:5;
y=1:50;
X = repmat(x',1,size(y,2)).';
Y = repmat(y',1,size(x,2));
Z = X./Y;
figure
hSurf = surf(Y,X,Z);
rotate(hSurf,[1,0,0],45) % As proposed by #ASantosRibeiro
colorbar
xlabel('x')
ylabel('y')
zlabel('z')
The repmat function is used to replicate both x and y in order to form X and Y with correct sizes to be allowed to divide one by the other in order to form Z. This operation is quite similar to the call to meshgrid in your code. For a small dataset like the one in your example the running times are similar with both methods (on my laptop), however when I use x = 1:0.1:500 and y = 1:500 for example the vectorized version takes 3x less time, so it might be worthwhile to look at it.
Hope that helps you!

Colormap 2D for a scalar - Matlab

i'm simulating a wave propogation in time and place. i want to make a colormap of its values for every time step, in space. i mean, i want to make a figure of 2 axes (x and y) and displays the wave's values at those points by color (the wave varible is V).
how can i do it?
i'v tried:
for ind1 = 1:length(t)
figure()
trisurf(x1,y1,V(:,ind1),'EdgeColor', 'None', 'facecolor', 'interp');
view(2);
end
but i got a message that z (=V) suppose to be a function and not a scalar.
any suggestions?
I have two options, I don't think they will be perfect, but it might help.
First, interpolate the data onto a rectangular mesh and use contourf:
F=scatteredInterp(x,y,V(:,ind1));
X=linspace(min(x),max(x));
Y=linspace(min(y),max(y));
contourf(X,Y,F(X,Y))
Secondly, use scatter to plot points with varying colour:
scatter(x,y,25,V(:,ind1))
where the 25 controls the size of each marker, you may have to experiment with it.
Hope that gives you some ideas.
i've made a loop that finally works:
clear heart_movie
Vnorm = mat2gray(V(:,1:2000));
x1_new = x1-min(x1)+1;
y1_new = y1-min(y1)+1;
for ind1 = 1:2000
heart = zeros(max(x1_new),max(y1_new));
z = Vnorm(:,ind1);
for ind2 = 1:length(z);
heart(y1_new(ind2),x1_new(ind2))= z(ind2);
end
colormap(jet);
imagesc(flipud(heart));
end

How to do an animated plot in matlab

I was wondering if anyone knew how to do an animation plot of
x = (dataset of 1000 points)
y = (dataset of 1000 points)
plot(x,y)
big problem is these are datasets that i am trying to plot , or x,y coordinates as opposed to a function which I would know how to plot via an animation.
I tried to do frames in a for loop but it gave me dots and didn't join them in a line graph so I couldn't really watch the path being traced out.
code I used was
for i = 1:length(DATASET1)
pause(0.1)
plot(DATASET1(i),DATASET2(i))
draw on
end
If what you want is for the plot to "grow" point by point: the easiest way is to create an empty plot and then update its XData and YData properties at each iteration:
h = plot(NaN,NaN); %// initiallize plot. Get a handle to graphic object
axis([min(DATASET1) max(DATASET1) min(DATASET2) max(DATASET2)]); %// freeze axes
%// to their final size, to prevent Matlab from rescaling them dynamically
for ii = 1:length(DATASET1)
pause(0.01)
set(h, 'XData', DATASET1(1:ii), 'YData', DATASET2(1:ii));
drawnow %// you can probably remove this line, as pause already calls drawnow
end
Here's an example1 obtained with DATASET1 = 1:100; DATASET2 = sin((1:100)/6);
1 In case someone's interested, the figure is an animated gif which can be created by adding the following code (taken from here) within the loop, after the drawnow line:
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if ii == 1;
imwrite(imind,cm,filename,'gif','Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
Looks like you were close. Not sure draw on is any command though.
See if the code here inspires you to solve your case -
%// Sample x and y values assumed for demo.
x = 1:1000;
y = x.^2;
%// Plot starts here
figure,hold on
%// Set x and y limits of the plot
xlim([min(x(:)) max(x(:))])
ylim([min(y(:)) max(y(:))])
%// Plot point by point
for k = 1:numel(x)
plot(x(k),y(k),'-') %// Choose your own marker here
%// MATLAB pauses for 0.001 sec before moving on to execue the next
%%// instruction and thus creating animation effect
pause(0.001);
end
Since R2014b, you can work with annimatedline object (doc and how-to) that is meant to handle animated graphs pretty well. Basically, the annimatedline object has a addpoints function that adds new points to the line without having to redefine the existing points, along with a clearpoints function that clears lines for more complex animations.
Here is an example:
h = animatedline;
axis([0,4*pi,-1,1])
x = linspace(0,4*pi,1000);
y = sin(x);
for k = 1:length(x)
addpoints(h,x(k),y(k));
drawnow
end

How to plot a second graph instead of color coding in matlab

i just started with my master thesis and i already am in trouble with my capability/understanding of matlab.
The thing is, i have a trajectory on a surface of a planet/moon whatever (a .mat with the time, and the coordinates. Then i have some .mat with time and the measurement at that time.
I am able to plot this as a color coded trajectory (using the measurement and the coordinates) in scatter(). This works awesomely nice.
However my problem is that i need something more sophisticated.
I now need to take the trajectory and instead of color-coding it, i am supposed to add the graph (value) of the measurement (which is given for each point) to the trajectory (which is not always a straight line). I will added a little sketch to explain what i want. The red arrow shows what i want to add to my plot and the green shows what i have.
You can always transform your data yourself: (using the same notation as #Shai)
x = 0:0.1:10;
y = x;
m = 10*sin(x);
So what you need is the vector normal to the curve at each datapoint:
dx = diff(x); % backward finite differences for 2:end points
dx = [dx(1) dx]; % forward finite difference for 1th point
dy = diff(y);
dy = [dy(1) dy];
curve_tang = [dx ; dy];
% rotate tangential vectors 90° counterclockwise
curve_norm = [-dy; dx];
% normalize the vectors:
nrm_cn = sqrt(sum(abs(curve_norm).^2,1));
curve_norm = curve_norm ./ repmat(sqrt(sum(abs(curve_norm).^2,1)),2,1);
Multiply that vector with the measurement (m), offset it with the datapoint coordinates and you're done:
mx = x + curve_norm(1,:).*m;
my = y + curve_norm(2,:).*m;
plot it with:
figure; hold on
axis equal;
scatter(x,y,[],m);
plot(mx,my)
which is imo exactly what you want. This example has just a straight line as coordinates, but this code can handle any curve just fine:
x=0:0.1:10;y=x.^2;m=sin(x);
t=0:pi/50:2*pi;x=5*cos(t);y=5*sin(t);m=sin(5*t);
If I understand your question correctly, what you need is to rotate your actual data around an origin point at a certain angle. This is pretty simple, as you only need to multiply the coordinates by a rotation matrix. You can then use hold on and plot to overlay your plot with the rotated points, as suggested in the comments.
Example
First, let's generate some data that resembles yours and create a scatter plot:
% # Generate some data
t = -20:0.1:20;
idx = (t ~= 0);
y = ones(size(t));
y(idx) = abs(sin(t(idx)) ./ t(idx)) .^ 0.25;
% # Create a scatter plot
x = 1:numel(y);
figure
scatter(x, x, 10, y, 'filled')
Now let's rotate the points (specified by the values of x and y) around (0, 0) at a 45° angle:
P = [x(:) * sqrt(2), y(:) * 100] * [1, 1; -1, 1] / sqrt(2);
and then plot them on top of the scatter plot:
hold on
axis square
plot(P(:, 1), P(:, 2))
Note the additional things have been done here for visualization purposes:
The final x-coordinates have been stretched (by sqrt(2)) to the appropriate length.
The final y-coordinates have been magnified (by 100) so that the rotated plot stands out.
The axes have been squared to avoid distortion.
This is what you should get:
It seems like you are interested in 3D plotting.
If I understand your question correctly, you have a 2D curve represented as [x(t), y(t)].
Additionally, you have some value m(t) for each point.
Thus we are looking at the plot of a 3D curve [x(t) y(t) m(t)].
you can easily achieve this using
plot3( x, y, m ); % assuming x,y, and m are sorted w.r.t t
alternatively, you can use the 3D version of scatter
scatter3( x, y, m );
pick your choice.
Nice plot BTW.
Good luck with your thesis.