Anonymous function context being destroyed during save/load cycle into a mat file.
6 views (last 30 days)
Show older comments
I'm creating a function handle in the code shown here, beneath the red lines:

- the anonymous function in the cell array takes 3 args, and passes them through to online_rpca_model_script2 with the argument parameter_instance already closed/closured by the outer anonymous function ( exp_f_handle ).
This at some point is saved into a .mat file. Before it is saved, I did a little jump into debug mode to poke around:

So the variable worker_args is the cell array that was created underneath the red lines in the first image. The worker_args{2} is the function handle in question, and I get the metadata for that function handle by calling "functions()" on it. I then look at all the element of the workspace field of the returned structure. We can see that everything that was in scope during the anonymous function's creation is there. Particularly, parameter_instance is in the 2nd element of the workspace field. I then save worker_args to a .mat file.
I then load this same .mat file in a second Matlab instance, which has exactly the same search path as the first Matlab instance. They are literally launched from the same directory, and I've run the same addpath script in both.

Then I call functions() on worker_args{2}, which is our function handle in question. We can see from the returned struct that the function strings match, the files match (you can't see that from the images, but they do), however: the workspace field has changed.
In particular, workspace{2} has lost the parameter_instance variable, workspace{3} has lost condor_obj and experiment_opts, and workspace{4} has lost a whole load of stuff. So now, when I call the function handle in worker_args{2}, it complains that the variable parameter_instance is undefined, despite that fact that it definitely was defined and closured according to the second image!
So something in the save-load process is screwing up my function handle, and I would like to know what I'm doing wrong, what I'm wrongly assuming, or what the bug is.
3 Comments
Matt J
on 24 Jun 2015
I don't quite know what you mean by "next outer scope". You mean the anonymous function is enclosed in a series of nested functions?
it doesn't explain why stuff gets wiped from the anon-functions context after a save-load.
Well, it might. There is sometimes a lot of useless data in the workspace of an anonymous function, stuff that never gets used and just serves to consume memory when you save the anonymous function to disk or export it elsewhere. This FEX file was my effort to try and clean this junk away,
http://www.mathworks.com/matlabcentral/fileexchange/45749-clean-anonymous-functions-of-excess-memory
It may be that R2015 introduced a similar attempt to do this, maybe not so successfully.
Answers (0)
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!