My code generates 10 videos in a for loop using the VideoWriter function. I know how to pre-allocate arrays but I am not sure how to make pre-allocation for VideoWriter files. writerObj file in the code below should be pre-allocated.
for i=1:10
images = cell(28,1);
writerObj(i) = VideoWriter(sprintf('bc_e2_ld_e5_time_history1/%d.avi',i));
writerObj(i).FrameRate = 1; % create the video writer with 1 fps
secsPerImage = ones(1,28); % set the seconds per image
open(writerObj(i)); % open the video writer
%-----------------------------Save images for making videos ---------------
for j=1:28
h=figure('visible','off');
p = pcolor(intrpVMStress1(:,:,j));
p.EdgeColor='none';
set(gca,'visible','off')
colormap('jet');
% colorbar;
axis equal;
title 'Von Mises Stress';
saveas(h,sprintf('bc_e2_ld_e5_time_history1/FIG%d.jpg',j)); % save the images
images{j}=imread(sprintf('bc_e2_ld_e5_time_history1/FIG%d.jpg',j)); % load the images
end
%-----------------------------convert the image to a frame ---------------
for u=1:length(images)
% convert the image to a frame
frame = im2frame(images{u});
for v=1:secsPerImage(u)
writeVideo(writerObj(i), frame);
end
end
close(writerObj(i));
end