In: Computer Science
MATLAB question: how do you write a video file using videowriter without displaying the current figure when doing getframe? I want to go through the data packet Xpixels x Ypixels x Zframes for each frame and write it to a video without showing the display until it's finally done. also I noticed if I turn the figure off the code runs much much slower using fig = figure(visible off). is there anyway to avoid this sluggishness? and speed it up without showing the figure?
  % Create a VideoWriter object to write the video out to a new, different file.
  writerObj = VideoWriter('NewRhinos.avi');
  open(writerObj);
  
  % Read the frames back in from disk, and convert them to a movie.
  % Preallocate recalledMovie, which will be an array of structures.
  % First get a cell array with all the frames.
  allTheFrames = cell(numberOfFrames,1);
  allTheFrames(:) = {zeros(vidHeight, vidWidth, 3, 'uint8')};
  % Next get a cell array with all the colormaps.
  allTheColorMaps = cell(numberOfFrames,1);
  allTheColorMaps(:) = {zeros(256, 3)};
  % Now combine these to make the array of structures.
  recalledMovie = struct('cdata', allTheFrames, 'colormap', allTheColorMaps)
  for frame = 1 : numberOfFrames
    % Construct an output image file name.
    outputBaseFileName = sprintf('Frame %4.4d.png', frame);
    outputFullFileName = fullfile(outputFolder, outputBaseFileName);
    % Read the image in from disk.
    thisFrame = imread(outputFullFileName);
    % Convert the image into a "movie frame" structure.
    recalledMovie(frame) = im2frame(thisFrame);
    % Write this frame out to a new video file.
    writeVideo(writerObj, thisFrame);
  end
  close(writerObj);
  % Get rid of old image and plot.
  delete(hImage);
  delete(hPlot);
  % Create new axes for our movie.
  subplot(1, 3, 2);
  axis off;  % Turn off axes numbers.
  title('Movie recalled from disk', 'FontSize', fontSize);
  % Play the movie in the axes.
  movie(recalledMovie);
  % Note: if you want to display graphics or text in the overlay
  % as the movie plays back then you need to do it like I did at first
  % (at the top of this file where you extract and imshow a frame at a time.)
  msgbox('Done with this demo!');
This is the code which will write video file using videowriter without displaying the current figure .
This doesn't uses fig = figure(visible off),rather you can follow what I have done in the code. This code doesn't have any speed issues and also it does the jobs.However, fig = figure(visible off) should also work just fine as it is also a very efficient way. You can try running other operations of during exexution if it is still slower.
Happy to help!