Out of memory. Type "help memory" for your options
Show older comments
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?
2 Comments
Walter Roberson
on 31 Mar 2020
Which MATLAB version are you using, and which operating system?
Veronika Mazulina
on 31 Mar 2020
Accepted Answer
More Answers (1)
Steven Lord
on 31 Mar 2020
0 votes
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.
2 Comments
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
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.
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!