Hi Naoise,
It is possible that the sheer volume of data being plotted is the root cause of the memory overflowing and CPU spikes. Please try the workarounds stated below:
- Avoid plots with illegibly large amounts of data. The million-line plots and the box plots with millions of outliers are consuming large amounts of memory. For plots like those, MATLAB is probably creating millions of objects in a complex hierarchy, which decreases performance and uses lots of memory.
- Clear the plots that you are finished with in order to free memory along the way.
- If you encounter memory-related issues even after removing the memory-hog plots as suggested above, it may be helpful separate the simulation and plotting into distinct steps, shutting MATLAB down in between.
The best way to do this is to start MATLAB from the command line, run your simulation, save its results, and exit. Then, for each plot, start MATLAB, load the results, create the plot, save, and exit. The link below explains how to start MATLAB from the command line:
You can use the '-r' option to execute MATLAB code. For example, you start MATLAB from the command line like so:
cd "C:\Program Files\MATLAB\R2020b\bin"
matlab -nodesktop -nodisplay -r "mySimulationScript; save('my_simulation_data.mat'); exit"
Please see the following documentation in order to address memory issues in MATLAB: