Clean up context of anonymous function handles - bug #328733

This submission provides a workaround for MATLAB bug #328733.
421 Downloads
Updated 1 Jul 2009

View License

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
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Startup and Shutdown in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.1.0.0

Fix: correctly handle non-anonymous function handles.

1.0.0.0