Create a .avi through VideoWriter

3 views (last 30 days)
Hello everyone,
I am trying to replicate the approach given in this example: https://es.mathworks.com/matlabcentral/answers/78345-make-a-smooth-animation
I want, as him, to not to show each frame in iteration of a loop, and get directly the .avi video. I have tried to addapt directly the code for my case, but it seems that avifile function does not work in my MatLab version (the last one). So I have seen that the role of avifile has been replaced by VideoWriter. The problem is... which function plays the role of addframe in the VideoWriter environment? I let you here my example of the code adapted directly from the aforementioned post
physical_time_inelastic=[0:1:30];
spatial_grid=[0:1:100];
data_set_inelastic=rand([length(spatial_grid),4,length(physical_time_inelastic)]);
avi_mx_inelastic=VideoWriter('mx_Magnetization_Component_Evolution_Inelastic.avi');
u9=figure('visible','off');
for i=1:length(physical_time_inelastic)
plot(spatial_grid.*(10^6),data_set_inelastic(:,2,i),'-b','LineWidth',2)
xlabel('Track position, $x \, \, \left( \mu\mathrm{m} \right)$','FontSize',14,'interpreter','latex')
ylabel('$x$-{\it th} magnetization components, $m_x$','FontSize',14,'interpreter','latex')
t9=title(['Inelastic collision, $t=$',num2str(physical_time_inelastic(i).*(10^(12))),' ps'],'FontSize',14,'interpreter','latex')
set(t9,'interpreter','latex','FontSize',12)
set(u9,'Units','Inches');
posu9=get(u9,'Position');
set(u9,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[posu9(3),posu9(4)])
avi_mx_inelastic=addframe(avi_mx_inelastic,u9);
end
avi_mx_inelastic=close(avi_mx_inelastic)
implay('mx_Magnetization_Component_Evolution_Inelastic.avi')
Also I would want to display in the title of each frame which is the value of physical_time_inelastic variable. It works as it is, but I would prefer the number to be in scientific notation. Any suggestion?
  2 Comments
Richard Wood
Richard Wood on 25 Feb 2020
See above that I have created the necessary matrices with random values, just to be used as an example.

Sign in to comment.

Accepted Answer

Delprat Sebastien
Delprat Sebastien on 25 Feb 2020
Well everything is in the doc and that's very easy. It is writeVideo.
Z = peaks; surf(Z); axis tight manual set(gca,'nextplot','replacechildren'); v = VideoWriter('peaks.avi'); open(v); for k = 1:20 surf(sin(2*pi*k/20)*Z,Z) frame = getframe(gcf); writeVideo(v,frame); end close(v);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by