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. |