Making video from an animation

2 次查看(过去 30 天)
I am plotting few graphs and trying to make movie out of it. The code runs without error. But unfortunately the video is not saved. Can anyone help me on this?
a = smoothed_final_plot_mat(:,3:end);
np = (size(a,2)-1)/2; % number of particles
t = a(2:end,1);
arena_image = imread('arena_pic.png');
h1 = axes ;
image('XData',[0 6890],'YData',[0 5000],'CData',arena_image) ; % set your limits
set(h1,'YDir','normal');
hold on
ax = gca;
ax.YDir = 'reverse';
axis([0 6890 0 5000])
h = zeros(1,np); % ants handles
hold on
writerObj = VideoWriter('output3.avi'); % Name it.
writerObj.FrameRate = 60; % How many frames per second.
open(writerObj);
for ii = 2:25:size(a,1) % frames
k = 0;
t = a(ii,1); % time of current frame
str = sprintf('Time in seconds = %f', t);
title(str)
label = cellstr(num2str(unique(a(1,2:end))'));
hold on
for jj = 2:2:np*2 % ant handles
x = a(ii,jj);
y = a(ii,jj+1);
k = k + 1;
h(k) = plot(x,y,'--or');
hi(k) = text(x, y, label(k), 'VerticalAlignment','bottom', 'HorizontalAlignment','right'); %#ok<SAGROW>
h2(k) = plot(a(max(1,ii-150):ii,jj), a(max(1,ii-150):ii,jj+1)); %#ok<SAGROW>
end
drawnow
pause(0.00001)
delete(h)
delete(hi)
delete(h2)
hold off
end
frame = getframe(gcf); % 'gcf' can handle if you zoom in to take a movie.
writeVideo(writerObj, frame);
close(writerObj);

采纳的回答

KSSV
KSSV 2019-5-8
YOur writevideo command is out side the loop..this should be inside the loop.

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by