MATLAB: Out of memory. Type “help memory” for your options

getframevideo

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?

Best Answer

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);