Clean up context of anonymous function handles - bug #328733
MATLAB saves some anonymous function handles with too much context information - see MATLAB bug #328733. In some cases, this will crash MATLAB when loading the function handle (e.g. if some large variables or objects that do not support serialisation are saved).
This function provides a workaround - it recreates the function handle without unnecessary context information.
Currently, there is one limitation: bound variables are not included correctly. E.g.
x = 1;
f = @(y)mod(y,x);
f1 = clean_funhandle(f);
f(2)
ans = 0
f1(2)
??? Undefined function or variable 'x'.
Error in ==> clean_funhandle>@(y)mod(y,x)
This is because x is not defined in the context of clean_funhandle.
The alternative version
f = @(y)mod(y,2);
f2 = clean_funhandle(f);
works. Here, the second argument is a constant, which is replaced correctly.
Cite As
Volkmar Glauche (2024). Clean up context of anonymous function handles - bug #328733 (https://www.mathworks.com/matlabcentral/fileexchange/24613-clean-up-context-of-anonymous-function-handles-bug-328733), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxCategories
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.