Path: news.mathworks.com!not-for-mail
From: "Bruno Luong" <b.luong@fogale.findmycountry>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Saving an anonymous function handle to a mat-file
Date: Fri, 12 Sep 2008 18:50:18 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 32
Message-ID: <gaedla$3iv$1@fred.mathworks.com>
References: <6iv5vcFn3i0U1@mid.individual.net> <gaebl1$sl0$1@fred.mathworks.com>
Reply-To: "Bruno Luong" <b.luong@fogale.findmycountry>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1221245418 3679 172.30.248.37 (12 Sep 2008 18:50:18 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 12 Sep 2008 18:50:18 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: news.mathworks.com comp.soft-sys.matlab:490040



This loadfun should be safer:

function fun___=loadfun(file)

data=load(file);
sfun___ = data.sfun;

clear file data;

evalvar(sfun___);

fun___ = sfun___.function;
clear sfun___;
fun___=eval(fun___);

end

% nested function
function evalvar(sfun)

for n=1:length(sfun.workspace)
    wn = sfun.workspace{n};
    vlist=fieldnames(wn);
    for k=1:length(vlist)
        val = wn.(vlist{k});
        assignin('caller',vlist{k}, val);
    end
end

end

% Bruno