Code covered by the BSD License  

Highlights from
Clean up context of anonymous function handles - bug #328733

Be the first to rate this file! 1 Download (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)

This submission provides a workaround for MATLAB bug #328733.

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

Contact us at files@mathworks.com