Make Animation in matlab and save animation in matlab

This is my code represent the my model
function dx = trainfinalmodel(t,x)
%Parameter Massa
m1 = 8095; % massa train set 1 dalam kg
m2 = 8500; % massa train set 2 dalam kg
g = 10;
%Parameter Gaya
f1 = 205.10^3; % dalam N
f2 = 302.10^3; % dalam N
c_0_1 = 0.01176;
c_1_1 = 0.00077616;
c_2_1 = 4.48 ;
c_0_2 = 0.01176 ;
c_1_2 = 0.00077616;
c_2_2 = 4.48;
v_0 = 300;
hstar = 120;
a_1 = -1./m1.*(c_1_1 + 2.*c_2_1.*v_0);
a_2 = -1./m2.*(c_1_2 + 2.*c_2_2.*v_0);
a_1_head = 1-(a_1.*hstar);
a_2_head = 1-(a_2.*hstar);
b = 1;
p_1 = -1./m1.*(c_0_1 - c_2_1.*(v_0).^2);
p_2 = -1./m2.*(c_0_2 - c_2_2.*(v_0).^2);
x = [x(1);x(2);x(3);x(4)];
A = [0 a_1_head 0 0;
0 0 0 0;
0 (a_2_head - 1) 0 a_2_head;
0 0 0 0
];
B = [-b.*hstar 0;
b 0;
0 -b.*hstar;
-b b
];
U_t = [f1; f2;];
W = [((a_1 - 1).*v_0) - (p_1.*hstar);
0;
((a_2 - 1).*v_0) - (p_2.*hstar);
((a_1 - 1).*v_0) - (p_1.*hstar);
];
dx = (A*x) + (B*U_t) + W;
end
This is the code for ode45
clear all, close all
tspan = [0:1:1000];
x0 = [0; 0; 100; 0];
figure(1)
[t,x] = ode45(@trainfinalmodel,tspan,x0);
plot(t,x)
legend('x1','x2','x3','x4')
title('Non Linear Model')
xlabel('Waktu(s)')
ylabel('Jarak(m)')
figure(2)
subplot(2,1,1);
hax1 = plot(t,x(:,1),t,x(:,3))
title('Posisi Rangkaian Kereta')
xlabel('Waktu(s)')
ylabel('Jarak(m)')
legend('x_1','x_3')
subplot(2,1,2);
hax2 = plot(t,x(:,2),t,x(:,4))
title('Kecepatan Rangkaian Kereta')
xlabel('Waktu(s)')
ylabel('Kecepatan(m/s)')
legend('x_2','x_4')
and this is the code for animation
a = 1:1000;
b = x(:,1);
c = x(:,3);
%%
p3 = figure(3);
for i = 1:length(a)
plot(b(1:i),'-r*');
hold on;
plot(c(1:i),'-b*');
title('Posisi Rangkaian Kereta')
xlim([1 1000])
xlabel('Waktu(s)')
ylabel('Jarak(m)')
legend('x_1','x_3')
fra = getframe(p3);
plot_i{i} = frame2im(fra);
[A{i},map{i}] = rgb2ind(plot_i{i},256);
end
%%
for i = 1:length(a)
imwrite(A{i},map{i},'check_gif.gif','gif','WriteMode','append',...
'DelayTime',2);
end
%%
d = 1:1000;
e = x(:,2);
f = x(:,4);
%%
p4 = figure(4);
for j = 1:length(d)
plot(e(1:j),'-r*');
hold on;
plot(f(1:j),'-b*');
title('Posisi Rangkaian Kereta')
xlim([1 1000])
xlabel('Waktu(s)')
ylabel('Jarak(m)')
legend('x_2','x_4')
fra = getframe(p4);
plot_j{j} = frame2im(fra);
[B{j},map{j}] = rgb2ind(plot_j{j},256);
end
%%
for j = 1:length(d)
imwrite(B{j},map{j},'check_gif1.gif','gif','WriteMode','append',...
'DelayTime',2);
end
from my animation code only figure(3) is run, figure(4) doesn't run, so I have 3 question
  1. How i run animation in figure(3) and figure(4) at the same time?
  2. How i save the animation?
  3. How i open the animation only without run the code, only in video?

Answers (1)

I already did that with this
fra = getframe(p3);
plot_i{i} = frame2im(fra);
[A{i},map{i}] = rgb2ind(plot_i{i},256);
based on your link but the problem, where i can find my file? and I have managed to do one chart but i need two graph run at the same time, in my code and my understanding it only figure(3) is run but figure (4) cannot Help me please. Thank you

10 Comments

Can you use subplot for two graphs at the same time?
in figure(2) just graph not animation, it can, but in animation cannot only x(:,1) and x(:,3) , the x(,2) and x(:,4) still missing
I don't understand you. What is the problem? Did you make an animation?
yes i make animation, but only x (:, 3) and x (:, 1) animation appear
I want all animation appear
I use subplot
a = 1:1000;
b = x(:,1);
c = x(:,3);
%%
p3 = figure(3);
This is subplot
subplot(2,1,1)
for i = 1:length(a)
plot(b(1:i),'-r*');
hold on;
plot(c(1:i),'-b*');
title('Posisi Rangkaian Kereta')
xlim([1 1000])
xlabel('Waktu(s)')
ylabel('Jarak(m)')
legend('x_1','x_3')
fra = getframe(p3);
plot_i{i} = frame2im(fra);
[A{i},map{i}] = rgb2ind(plot_i{i},256);
end
%%
for i = 1:length(a)
imwrite(A{i},map{i},'check_gif.gif','gif','WriteMode','append',...
'DelayTime',2);
end
%%
d = 1:1000;
e = x(:,2);
f = x(:,4);
%%
and this is subplot
subplot(2,1,2)
for j = 1:length(d)
plot(e(1:j),'-r*');
hold on;
plot(f(1:j),'-b*');
title('Posisi Rangkaian Kereta')
xlim([1 1000])
xlabel('Waktu(s)')
ylabel('Jarak(m)')
legend('x_2','x_4')
fra = getframe(p4);
plot_j{j} = frame2im(fra);
[B{j},map{j}] = rgb2ind(plot_j{j},256);
end
%%
for j = 1:length(d)
imwrite(B{j},map{j},'check_gif1.gif','gif','WriteMode','append',...
'DelayTime',2);
end
But still can not all animation appear
still only x(:,1) and x(:,3) appear
The problem only x(:,1) and x(:,3) is appear, i want animation x(:,1),x(:,3) in subplot (2,1,1) and x(:,2), x(:,4) in subplot(2,1,2)
Adapt this simple examle for your needs
[x,y] = pol2cart(deg2rad(0:2:360),1);
x1 = linspace(0,20,length(x));
y1 = sin(x1);
subplot(121)
h1 = plot(x,y);
axis tight equal
subplot(122)
h2 = plot(x1,y1);
axis tight equal
for i = 1:length(x)-1
set(h1,'xdata',x(1:i),'ydata',y(1:i))
set(h2,'xdata',x1(1:i),'ydata',y1(1:i))
pause(0.02)
end
I have learned form your code, Is it like this?
a = 1:1000;
b = x(:,1);
c = x(:,3);
%%
p3 = figure(3);
for i = 1:length(a)
subplot(2,1,1)
plot(b(1:i),'-r*');
hold on;
plot(c(1:i),'-b*');
title('Posisi Rangkaian Kereta')
xlim([1 1000])
xlabel('Waktu(s)')
ylabel('Jarak(m)')
legend('x_1','x_3')
fra = getframe(p3);
plot_i{i} = frame2im(fra);
[A{i},map{i}] = rgb2ind(plot_i{i},256);
end
%%
for i = 1:length(a)
imwrite(A{i},map{i},'check_gif.gif','gif','WriteMode','append',...
'DelayTime',2);
end
d = 1:1000;
e = x(:,2);
f = x(:,4);
for j = 1:length(d)
subplot(2,1,2)
plot(e(1:j),'-r*');
hold on;
plot(f(1:j),'-b*');
title('Posisi Rangkaian Kereta')
xlim([1 1000])
xlabel('Waktu(s)')
ylabel('Jarak(m)')
legend('x_2','x_4')
fra = getframe(p3);
plot_j{j} = frame2im(fra);
[B{j},map{j}] = rgb2ind(plot_j{j},256);
end
%%
for j = 1:length(d)
imwrite(B{j},map{j},'check_gif1.gif','gif','WriteMode','append',...
'DelayTime',2);
end
still only x(:,1) and x(:,3) is appear. Help me please
if it can't be a subplot, then x(:,2), x(:,4) in figure 4 also doesn't matter

Sign in to comment.

Categories

Products

Release

R2020a

Asked:

on 29 Jun 2020

Commented:

on 1 Jul 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!