MATLAB: Storing data in RAM

comsol with matlabstoring data in ram

Hello
I am using MATLAB in conjunction with a finite-element simulation program (COMSOL) that solves for some physics with a time-stepping method. How it works is basically for each time-step in COMSOL, it executes my MATLAB function and returns the output. My MATLAB function is modeling a complex process and its output depends on many variables from within my function from the previous timestep. However, variables from inside of a function are lost after the function returns its output, so I need to store them somewhere. I have been saving them to a .mat file at the end of my function call and loading them from that .mat file at the start of my function call as a means to communicate this data between the different timesteps. This method was working fine for 2-D finite element simulations, however in 3-D simulations the variable sizes are much larger and the saving loading time becomes too large.
My variables.mat file that I am loading is ~25 MB, containing 2 structures, 2 1×1 doubles, a 1×1 scatteredInterpolant, a 200×1 double, a 50000×1 double, and a 50000×200 double. It takes approximately 17 seconds to load this .mat file from the hard drive and 1 second to save it (on a computer with 64GB RAM and dual Intel Xeon X5690 processors). These loading times are far too large for my simulation to run in a reasonable amount of time, as COMSOL may need to compute hundreds of timesteps.
I am looking for a way to store these variable in the RAM somehow, rather than saving or loading from the hard drive. Or possibly find a way to keep variables from a function saved in the workspace, although I'm not sure this method would work because COMSOL may be opening a "new" MATLAB session for each time-step/function call.
Is there a way to keep track of 6 or 7 variables from within a function, other than to load and save them from the hard drive?
Any help is greatly appreciated, Thanks!

Best Answer

doc persistent
?
Related Question