Need to store data every loop

3 views (last 30 days)
Andrei
Andrei on 22 Feb 2023
Commented: Andrei on 22 Feb 2023
I have a program which counts some physical process. I discretize space and time and that means that I have a lot off poits of calculation. I store video records of system evolution every loop (loop changes some parameter). I need to store mean values from each loop and after loops I build mean vs parameter value. But for bigger number of points I get error Out of memory. How to clear variables every loop so that I save mean value?

Accepted Answer

Walter Roberson
Walter Roberson on 22 Feb 2023
I will not run the program to that point because of some issues that I recommend be fixed first.
Your code has
for p = range2
eps = zeros(length(range));
res = zeros(length(range));
count = 1;
for eps3 = range
and inside eps3 you have
writerObj = VideoWriter('E:\УЧЕБА\ИТМО\Science\Res\linear.mp4', 'MPEG-4');
so for each different p value and each different eps3 value, you overwrite all of the same video. That seems like a waste of time -- if you only want the last result then only generate the last result.
I recommend by the way that you store the E:\УЧЕБА\ИТМО\Science\Res part in some variable before the for p loop, and that when you go to open the video object that you use fullfile to construct the output file name. This will make it easier for other people to modify the program for their own system. On my non-Windows system for example, it created a file literally named E:\УЧЕБА\ИТМО\Science\Res\linear.mp4 complete with literal E colon backslash and all. Writing more portable code is a Good Thing.
% Initialization code
workspace;
You already opened the workspace, no need to re-open it inside the loop. Especially since workspace will not update until you drop into the debugger or return to the command line.
hFigure = figure;
You create figures in a loop, but you never delete those figures. That will cause figures to accumulate -- taking up memory.
surf(x,y,z);
...
thisFrame = getframe(gcf);
Those kinds of lines are a problem. The code assumes that the current axes is the same one as the cla reset; and that the current figure is the same as the hFigure = figure . However, those are not necessarily true. If you click on any MATLAB figure while the code is running, then that becomes the current figure, and it's CurrentAxes property becomes the current axes. To get proper behaviour you would have to keep your hands off of the keyboard and mouse as long as the program is running -- which hs probably not realistic.
You should code all of your graphics calls to take a record of the current figure and current axes, and pass the appropriate value to all graphics routines so that they know exactly what they should work on. For example,
hFigure = figure;
ax = axes('Parent', hFigure);
set(hFigure, 'renderer', 'zbuffer');
...
cla(ax, 'reset');
...
surf(ax, x,y,z);
colormap(ax, hot);
axis(ax, 'tight')
zlim(ax, [0, 1.5]);
...
shading(ax, 'interp');
...
grid(ax, 'off');
...
thisFrame = getframe(hFigure);
but also for the other calls such as title() -- if a graphics call permits an ax passed in or permits a 'Parent' option, then be sure to pass in the appropriate parameter.
and either delete hFigure when you are finished with it -- or else create hFigure and ax before the for p loop and just use them all throughput instead of generating new figures and new axes all of the time.
Making sure to use the parent ensures that graphics do not get placed in the wrong location even if you click on windows and move them around or minimize them -- and the discipline of writing parents consistently tends to lead to better recognition of where to re-use existing graphics objects, which reduces the chances that you will leave orphan graphics objects existing taking up memory but no longer of interest. Reducing, in other words, the chances that you will run out of memory.
  1 Comment
Andrei
Andrei on 22 Feb 2023
Thank you very much for spent time. These are very helpful remarks, will fix all

Sign in to comment.

More Answers (0)

Categories

Find more on Labels and Annotations 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!