Out of memory. Type "help memory" for your options

3 views (last 30 days)
Hello everyone!
I try to make visualisation of the robots motion and record it in video file. The code looks as follows:
%% Visualisation
for ii=1:length(VisualisationArray)
center_x = VisualisationArray(ii,1:4:RobotsNum*4);
center_y = VisualisationArray(ii,2:4:RobotsNum*4);
orientation = VisualisationArray(ii,3:4:RobotsNum*4);
[x,y] = triangle(center_x, center_y, orientation);
% h.FaceColor = Agent(1).Structure.Position.state(ii);
set(h,'Xdata',x,'Ydata',y);
F(ii) = getframe(gcf);
pause(0.001)
end
%% Write a video
writerObj = VideoWriter('myVideo.avi');
writerObj.FrameRate = 10;
% open the video writer
open(writerObj);
% write the frames to the video
for i=1:length(F)
% convert the image to a frame
frame = F(i) ;
writeVideo(writerObj, frame);
end
% close the writer object
close(writerObj);
However, if VisualisationArray is too long I recieve the next error message:
Out of memory. Type "help memory" for your options.
Error in matlab.graphics.internal.getframeWithDecorations (line 26)
u = getFrameImage(c, withDecorations);
Error in alternateGetframe
Error in getframe (line 136)
x = alternateGetframe(parentFig, offsetRect, scaledOffsetRect, includeDecorations);
Error in Animation (line 17)
F(ii) = getframe(gcf);
How to deal with it? Maybe there are another options to record frames video?

Accepted Answer

Ameer Hamza
Ameer Hamza on 31 Mar 2020
I guess for large visualization, and somehow the matrix F becomes very large. Why not just write each frame at the time it is created. It will completely avoid the creation of the array F.
% % Visualisation
%% Write a video
writerObj = VideoWriter('myVideo.avi');
writerObj.FrameRate = 10;
% open the video writer
open(writerObj);
for ii=1:length(VisualisationArray)
center_x = VisualisationArray(ii,1:4:RobotsNum*4);
center_y = VisualisationArray(ii,2:4:RobotsNum*4);
orientation = VisualisationArray(ii,3:4:RobotsNum*4);
[x,y] = triangle(center_x, center_y, orientation);
% h.FaceColor = Agent(1).Structure.Position.state(ii);
set(h,'Xdata',x,'Ydata',y);
% convert the image to a frame
frame = getframe(gcf);
% write the frames to the video
writeVideo(writerObj, frame);
pause(0.001)
end
% close the writer object
close(writerObj);

More Answers (1)

Steven Lord
Steven Lord on 31 Mar 2020
Rather than keeping all the data for all the frames in memory and writing the video all at once why not open the VideoWriter object before the for loop then visualize, capture, and write each frame inside the loop body? Then after the for loop is complete close the VideoWriter object.
See the "Create AVI File from Animation" example on the writeVideo documentation page as a model.
  2 Comments
David Russell
David Russell on 15 Sep 2022
Is there any difference in the final result between calling writeVideo after the loop vs. in the loop? E.g. does each call to writeVideo actually compress the individual frame, or just save it uncompressed to disk so that the whole video can be compressed when the object is closed?
Walter Roberson
Walter Roberson on 15 Sep 2022
That is a good question. Mathworks does not publicly define the situations under which compression is done.
The code inside videoWriter() does not explicitly do any compression. But the code calls internal routines to write one frame at a time, and has an comment about the data pipe buffering 10 frames, and has an internal crosscheck about whether that buffer became full (I think.. the logic could be more clear.)
I would deduce from the code that compression is probably not happening at the time that close is done, that instead the internal workings buffer up to 10 frames at a time and compress as they go.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!