How to adjust my video to only save 30 fps while in my for loop that goes upto 20000 iterations?

5 views (last 30 days)
My video is created to about 5.5 mins and I am trying to make the video to be about just 20 seconds with 30 fps.
% Video Making:
VidFile = VideoWriter('Part1movie.mp4','MPEG-4');
VidFile.FrameRate = 30;
open(VidFile);
% 5 pt stencil:
stencil = 5;
% change in time:
dt = 1e-3;
% time vector and time iteration size.
t = t0:dt:tf;
mt = length(t);
% Plotting initial:
figure (1)
imagesc(phi)
% Material A is 1 and Material B is -1
colorbar
caxis([-1 1])
title(['1st Order Euler Field Distribution at stencil = ' num2str(stencil) ', dt = ' num2str(dt) 's, and t = ' num2str(t0) 's.'],'FontSize',18)
set(gca,'LineWidth',1,'FontSize',8)
axis equal
for k = 1:mt-1
% Eqn 4:
v1 = Laplacian_2D(phi,h,stencil);
v2 = (b^4 * phi.^3 - a * b^2 * phi) - gamma * v1;
v3 = D * Laplacian_2D(v2,h,stencil);
newphi = phi + dt * v3;
phi = newphi;
if t(k) == 5
% Plotting at 5 seconds:
figure (2)
imagesc(phi)
colorbar
caxis([-1 1])
title(['1st Order Euler Field Distribution at stencil = ' num2str(stencil) ', dt = ' num2str(dt) 's, and t = ' num2str(t(k)) 's.'],'FontSize',18)
set(gca,'LineWidth',1,'FontSize',8)
axis equal
elseif t(k) == 10
% Plotting at 10 seconds:
figure (3)
imagesc(phi)
colorbar
caxis([-1 1])
title(['1st Order Euler Field Distribution at stencil = ' num2str(stencil) ', dt = ' num2str(dt) 's, and t = ' num2str(t(k)) 's.'],'FontSize',18)
set(gca,'LineWidth',1,'FontSize',8)
axis equal
elseif t(k) == 15
% Plotting at 15 seconds:
figure (4)
imagesc(phi)
colorbar
caxis([-1 1])
title(['1st Order Euler Field Distribution at stencil = ' num2str(stencil) ', dt = ' num2str(dt) 's, and t = ' num2str(t(k)) 's.'],'FontSize',18)
set(gca,'LineWidth',1,'FontSize',8)
axis equal
elseif t(k) == 19.999
% Plotting at 20 seconds:
figure (5)
imagesc(phi)
colorbar
caxis([-1 1])
title(['1st Order Euler Field Distribution at stencil = ' num2str(stencil) ', dt = ' num2str(dt) 's, and t = ' num2str(t(k)) 's.'],'FontSize',18)
set(gca,'LineWidth',1,'FontSize',8)
axis equal
elseif t(k) <= 10
% Plotting the first 10 seconds to make the video:
figure (6)
imagesc(phi)
colorbar
caxis([-1 1])
title(['1st Order Euler Field Distribution at stencil = ' num2str(stencil) ', dt = ' num2str(dt) 's, and t = ' num2str(t(k)) 's.'],'FontSize',18)
set(gca,'LineWidth',1,'FontSize',8)
axis equal
Fr(k + 1) = getframe(gcf);
writeVideo(VidFile,Fr(k + 1))
end
end
close(VidFile);

Accepted Answer

darova
darova on 14 Aug 2019
Edited: darova on 14 Aug 2019
If you want 20 sec video with 30 fps you just need to write 600 frames
For dt=0.001 it is 10000 frames for 10 sec simulation so: 10000/600 = 16.66
Write each 16.66th frame
k1 = 1;
for k = 1:mt-1
% Eqn 4:
% ...
% get 17, 33, 50, 67 ... frames
elseif t(k) <= 10 && k == round(k1*100/6)
k1 = k1 + 1;
% actions
end
end
By the way, your code can be shorter
% create 6 figures with the same properties
for i = 1:6
figure(i)
colorbar
caxis([-1 1])
title(['1st Order Euler Field Distribution at stencil = ' num2str(stencil) ', dt = ' num2str(dt) 's, and t = ' num2str(t(k)) 's.'],'FontSize',18)
set(gca,'LineWidth',1,'FontSize',8)
axis equal
end
k1 = 1;
for k = 1:mt-1
% Eqn 4:
% ...
if t(k) == 5
% Plotting at 5 seconds:
figure (2)
imagesc(phi)
elseif t(k) == 10
% Plotting at 10 seconds:
figure (3)
imagesc(phi)
elseif t(k) == 15
% Plotting at 15 seconds:
figure (4)
imagesc(phi)
elseif t(k) == 19.999
% Plotting at 20 seconds:
figure (5)
imagesc(phi)
elseif t(k) <= 10 && k == round(k1*100/6) % get 17, 33, 50, 67 ... frames
% Plotting the first 10 seconds to make the video:
k1 = k1 + 1;
figure (6)
imagesc(phi)
Fr = getframe(gcf);
writeVideo(VidFile,Fr)
end
end
  4 Comments
Walter Roberson
Walter Roberson on 15 Aug 2019
Add
hold on
after the axis equal call.
You might also need to set the CData property of the image object instead of calling imagesc each time.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!