persistent variable precludes simbiology acceleration, how can I keep an array available in memory
2 views (last 30 days)
In a simbiology model, I use a spline function in a matlab .m function to set the value of a variable parameter. For example, the assignment rule might be: glucose = forced_glucose(time) The function is called a lot, and I'd like to minimize overhead. The data for the spline is in an xlsx file, and I'd like the function to only read the file once, and then retain the data in memory until I clear the function. Using the following works very well when one is running the model and developing stuff and testing constructs:
function glu = forced_glu(time)
raw = xlsread('my data file.xlsx')
pp = spline(raw(:,1),raw(:,2));
glu = ppval(pp,time);
Unfortunately, when one uses sbiofit, a warning is flagged noting that the model could not be accelerated. Apparently this is a limitation in the accelerate function, and is not surmountable. The limitation IS noted in the documentation set for "accelerate" where one is warned not to use persistent variables in functions used by simbiology, but there is no warning in the docs for "persistent".
I've been goofing around a bit trying to see if the global construct would work. There were a few glitches in my approach. I thought that someone might have run into this. Is there a way to ensure that a function used in simbiology rate expressions has a way to initialize on the first call, and to keep a structure (the piecewise polynomial structure 'pp') in accessible memory while running the model? I really don't want to process date into splines every call (several calls per time point).
In MATLAB, I could make the function use the pp as both an input and an output argument. But would that work with SB repeat assignment rules? Can I say [simbio_parameter_value pp] = forced_glucose(time,pp), and have the output pp stored in matlab memory?
Arthur Goldsipe on 2 Apr 2018
This is a timely question. The note about persistent variables being incompatible with acceleration is incorrect. (I think that used to be a limitation?) I do still encourage you to use them with caution, but pre-loading data for interpolation is a perfectly valid use of persistent variables. And I was already working with my doc writer to clarify this information and add an example of interpolation.
The most up-to-date information on what functions and language features that are supported can be found here. xlsread is not listed, so I didn't think it was supported. Your comments suggest it's supported, but I can't reproduce that on my Mac. To be safe, I suggest using the load command instead. We do document the limitations you're seeing with spline here.
I definitely recommend using a persistent variable rather than a global variable and using load instead of xlsread. I suggesting writing a separate helper function that loads the data using xlsread, creates a piecewise polynomial, and then saves it to the MAT file that you load into the persistent variable.
Finally, if you are able to reproduce the crash, please reach out. That's definitely something we would want to fix ASAP.