Be the first to rate this file! 18 downloads (last 30 days) File Size: 1.75 KB File ID: #24613

Clean up context of anonymous function handles - bug #328733

by Volkmar Glauche

 

01 Jul 2009 (Updated 01 Jul 2009)

Code covered by BSD License  

This submission provides a workaround for MATLAB bug #328733.

Download Now | Watch this File

File Information
Description

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.

MATLAB release MATLAB 7.8 (R2009a)
Zip File Content  
Other Files clean_funhandle.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
02 Jul 2009 us

note: since r2009a you can do this

     f1=@(x) mod(x,2);
     f2=str2func(func2str(f1));

- the private workspace issue is the same...

us

08 Jul 2009 Volkmar Glauche

This should actually work in older releases, too. My focus was on the eval() solution because the idea came from http://www.mathworks.com/matlabcentral/fileexchange/24447, where I generate code for arbitrary variables. This code of course needs to be evaluated.

Please login to add a comment or rating.
Updates
01 Jul 2009

Fix: correctly handle non-anonymous function handles.

Tag Activity for this File
Tag Applied By Date/Time
bug Volkmar Glauche 01 Jul 2009 12:34:17
function handle Volkmar Glauche 01 Jul 2009 12:34:18
function Volkmar Glauche 01 Jul 2009 12:34:18
workaround Volkmar Glauche 01 Jul 2009 12:34:18
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com