Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Saving an anonymous function handle to a mat-file
Date: Fri, 12 Sep 2008 17:52:01 +0000 (UTC)
Organization: Xoran Technologies
Lines: 28
Message-ID: <gaea81$8ve$1@fred.mathworks.com>
References: <6iv5vcFn3i0U1@mid.individual.net> <gae6t8$7de$1@fred.mathworks.com> <6ivm4rFp3vmU1@mid.individual.net> <gae9fm$r7n$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1221241921 9198 172.30.248.35 (12 Sep 2008 17:52:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 12 Sep 2008 17:52:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: news.mathworks.com comp.soft-sys.matlab:490031



"Matt " <mjacobson.removethis@xorantech.com> wrote in message <gae9fm$r7n$1@fred.mathworks.com>...
> 
> > This won't work unless I also save all variables involved in the 
> > anonymous function (in this case it's only "y"), because "y" will be 
> > undefined (or even worse: have another value) when I reload foobar.mat 
> > and evaluate "fstr".


I think I misunderstood you earlier.

No, when you execute

>>f=eval(fstr)

the result is

f = 

    @(x)(x+y)


In other words, all that happens is that fstr is converted from a string back to an anonymous function, f. It does not matter whether y exists in memory, at that point.

Of course, if you then try to evaluate the anonymous function f(x), you will get an error unless you first provide y, but presumably you have a way of doing that. Otherwise, why would you want to re-use the function on a different machine?