Making movies with subplot.

7 visualizzazioni (ultimi 30 giorni)
Guilherme Rocha
Guilherme Rocha il 28 Nov 2015
Risposto: Guilherme Rocha il 28 Nov 2015
I've been trying to export an animation with subplots. So far, the code is:
syms t;
l1 = 38;
l2 = 27;
l3 = 09;
T43 = [ 1 0 0 0 ; 0 1 0 0 ; 0 0 1 l1 ; 0 0 0 1 ];
T76 = [ 1 0 0 0 ; 0 1 0 0 ; 0 0 1 l2 ; 0 0 0 1 ];
T109= [ 1 0 0 0 ; 0 1 0 0 ; 0 0 1 l3 ; 0 0 0 1 ];
video = VideoWriter('newfile.mp4','MPEG-4');
open(video);
for t = 1:1:100
teta1 = pi+pi/4*(cos((pi/50)*t)-1); % ponto de referência = pi (0) -- variação = ( 0 -- -180 )
teta2 = (pi/2)*sin((pi/50)*t); %(7/12)*sin((pi/50)*t); % ponto de referência = 0 (0) -- variação = (30 -- 0 -- -180 )
teta3 = 0; % ponto de referência = 0(0) -- variação = (-90 -- 0 -- 90 )
teta4 = pi/4*(cos((pi/50)*t)-1); % ponto de referência = 0 (0) -- variação = (0 -- -150 )
teta5= 0; % ponto de referência = 0 (0) -- variação = (-90 -- 0 -- 90 )
teta6= 0; % ponto de referência = 0 (0) -- variação = (-90 -- 0 -- 90 )
teta7= 0; % ponto de referência = 0 (0) -- variação = (-30 -- 30 )
R10 = [ 1 0 0 0 ; 0 cos(teta1) -sin(teta1) 0 ; 0 sin(teta1) cos(teta1) 0 ; 0 0 0 1 ];
R21 = [ cos(teta2) 0 -sin(teta2) 0 ; 0 1 0 0 ; sin(teta2) 0 cos(teta2) 0 ; 0 0 0 1];
R32 = [ cos(teta3) sin(teta3) 0 0 ; -sin(teta3) cos(teta3) 0 0 ; 0 0 1 0 ; 0 0 0 1 ];
R54 = [ cos(teta4) 0 -sin(teta4) 0 ; 0 1 0 0 ; sin(teta4) 0 cos(teta4) 0 ; 0 0 0 1];
R65 = [ cos(teta5) sin(teta5) 0 0 ; -sin(teta5) cos(teta5) 0 0 ; 0 0 1 0 ; 0 0 0 1 ];
R87 = [ 1 0 0 0 ; 0 cos(teta6) -sin(teta6) 0 ; 0 sin(teta6) cos(teta6) 0 ; 0 0 0 1 ];
R98 = [ cos(teta7) 0 -sin(teta7) 0 ; 0 1 0 0 ; sin(teta7) 0 cos(teta7) 0 ; 0 0 0 1];
A = [0 0 0];
Br = R10*R21*R32*T43;
B = [ Br(1,4) Br(2,4) Br(3,4)];
Cr=R10*R21*R32*T43*R54*R65*T76;
C = [ Cr(1,4) Cr(2,4) Cr(3,4)];
Dr = R10*R21*R32*T43*R54*R65*T76*R87*R98*T109;
D = [ Dr(1,4) Dr(2,4) Dr(3,4)];
subplot(2,2,1)
plot3( [A(1),B(1)] , [A(2),B(2)] , [A(3),B(3)] , 'k-o' , 'LineWidth' , 1.5 )
hold on
plot3( [B(1),C(1)] , [B(2),C(2)] , [B(3),C(3)] , 'b-o' , 'LineWidth' , 1.5 )
hold on
plot3( [C(1),D(1)] , [C(2),D(2)] , [C(3),D(3)] , 'r-o' , 'LineWidth' , 1.5 )
xlabel('x (cm)') , ylabel('y (cm)') , zlabel('z (cm)')
title ( 'posição de referência ')
text(A(1),A(2),A(3),' A')
text(B(1),B(2),B(3),' B')
text(C(1),C(2),C(3),' C')
text(D(1),D(2),D(3),' D')
axis ([-100 100 -100 100 -100 100])
grid on
subplot(2,2,2)
plot( [A(1),B(1)] , [A(2),B(2)] , 'k-o' , 'LineWidth' , 1.5 )
hold on
plot( [B(1),C(1)] , [B(2),C(2)] , 'b-o' , 'LineWidth' , 1.5 )
hold on
plot( [C(1),D(1)] , [C(2),D(2)] , 'r-o' , 'LineWidth' , 1.5 )
xlabel('x (cm)') , ylabel('y (cm)')
title ( 'plano XY ')
text(A(1),A(2),' A')
text(B(1),B(2),' B')
text(C(1),C(2),' C')
text(D(1),D(2),' D')
axis ([-100 100 -100 100 ])
grid on
subplot(2,2,3)
plot( [A(1),B(1)] , [A(3),B(3)] , 'k-o' , 'LineWidth' , 1.5 )
hold on
plot( [B(1),C(1)] , [B(3),C(3)] , 'b-o' , 'LineWidth' , 1.5 )
hold on
plot( [C(1),D(1)] , [C(3),D(3)] , 'r-o' , 'LineWidth' , 1.5 )
xlabel('x (cm)') , ylabel('z (cm)')
title ( 'plano XZ ')
text(A(1),A(3),' A')
text(B(1),B(3),' B')
text(C(1),C(3),' C')
text(D(1),D(3),' D')
axis ([-100 100 -100 100 ])
grid on
subplot(2,2,4)
plot( [-A(2),-B(2)] , [A(3),B(3)] , 'k-o' , 'LineWidth' , 1.5 )
hold on
plot( [-B(2),-C(2)] , [B(3),C(3)] , 'b-o' , 'LineWidth' , 1.5 )
hold on
plot( [-C(2),-D(2)] , [C(3),D(3)] , 'r-o' , 'LineWidth' , 1.5 )
xlabel('y (cm)') , ylabel('z (cm)')
title ( 'plano YZ ')
text(-A(2),A(3),' A')
text(-B(2),B(3),' B')
text(-C(2),C(3),' C')
text(-D(2),D(3),' D')
axis ([-100 100 -100 100 ])
grid on
M(t) = getframe();
writeVideo(video,M);
clf
end
close(video);
The problem is that the exported mp4 file shows only the last subplot. Any help would be greatly appreciated, thanks in advance!

Risposta accettata

Image Analyst
Image Analyst il 28 Nov 2015
Apparently by default it's getting the current axes, gca. Try getting the current figure, gcf:
thisFrame = getframe(gcf);
writeVideo(video, thisFrame);
No need to save the frame. Even if you did you'd have to use a cell array, not a regular numerical array like M, so you'd have to use braces not parentheses, but like I said there's no need to do that.

Più risposte (1)

Guilherme Rocha
Guilherme Rocha il 28 Nov 2015
Thank you so much. It worked perfectly.

Categorie

Scopri di più su Animation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by