Using VideoWriter to make a Movie without Displaying the Figure each time

11 ビュー (過去 30 日間)
Mark Fasano
Mark Fasano 2022 年 4 月 4 日
回答済み: Nithin 2025 年 2 月 14 日 9:33
I have a variable called movie which is a multidimensional array of a solution to a partial differential equation at every kth time step. I now want to use VideoWriter to make a movie out of this variable. However, it seems to take forever in run-time because Matlab is actually displaying each frame on the screen. Is there anyway to speed this up? My code for this section is attached below:
%%
clf;
vid = VideoWriter('movie_2D.avi');
vid.FrameRate = 20;
open(vid);
frames = 1000;
for i=1:frames
figure(1);
surf(X,Z,movie(:,:,1+(t_max/frames)*i));
colormap parula; shading interp; grid off; colorbar; caxis([-1,1]);
xlim([-x_Max, x_Max])
ylim([0, 1])
% title('N=%d, M=%d, \Delta t=$d, \lambda_0=%d, \lambda_H=%d',N,M,dt,lambda0,lambdaH)
title('N=64, M=32, \Delta t=.0001, \lambda_0=100, \lambda_H=10')
xlabel('x')
ylabel('y')
zlabel('u')
view(0,90);
fig = gcf;
frame = getframe(fig);
writeVideo(vid,frame);
end
close(vid)

回答 (1 件)

Nithin
Nithin 2025 年 2 月 14 日 9:33
Consider using the 'Visible' - 'off' property to stop displaying the figure.
fig = figure('Visible', 'off');
Leverage the following documentation to learn more about the ‘Visible’ parameter for the ‘figure’ class: https://www.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html#buiwuyk-1_sep_mw_7bf9e6a7-1c7a-4065-98b1-e283df705a1d
This way you can stop displaying the image for every frame.
However, the delay in execution isn't primarily due to the figure being displayed each time. The real issue is the creation and updating of the figure for each frame. You can speed up the process by moving 'figure(1)' outside the loop to avoid creating new figures repeatedly. Additionally, consider these optimization techniques to enhance performance:
  • Ensuring that arrays or matrices used in your loop are preallocated. This can prevent MATLAB from reallocating memory during each iteration.
  • Using the 'getframe' function efficiently by capturing only the necessary parts of the figure. Leverage the following documentation to learn more about the ‘getframe’ function: https://www.mathworks.com/help/matlab/ref/getframe.html
  • Updating properties of the figure using the 'set' function.
For more information on how to write efficient code, refer to the following documentation:
I've created a sample MATLAB script with dummy data to demonstrate execution times for different scenarios, which is attached for your reference.
Refer the image below to better understand the execution times for all three cases.
The results clearly show that simply disabling figure visibility (Case 1 and Case 2) doesn't significantly improve execution time. However, by implementing the suggested techniques, there's a notable reduction in execution time (Case 3).
I hope this resolves your issue.

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by