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 16:55:04 +0000 (UTC)
Organization: Xoran Technologies
Lines: 39
Message-ID: <gae6t8$7de$1@fred.mathworks.com>
References: <6iv5vcFn3i0U1@mid.individual.net>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1221238504 7598 172.30.248.38 (12 Sep 2008 16:55:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 12 Sep 2008 16:55:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: news.mathworks.com comp.soft-sys.matlab:490024



Christian Zietz <newsgroup@chzsoft.com.ar> wrote in message <6iv5vcFn3i0U1@mid.individual.net>...
> Hi,
> 
> when I enter
> 
> y = 1;
> f = @(x)(x+y);
> save foobar f;
> 
> in the MATLAB command window, the resulting foobar.mat will load and 
> work when copied to another computer.
> 
> When I execute the same code inside a function and copy foobat.mat, 
> MATLAB on the other computer needs the m-file containing the function 
> where I defined the function handle "f" to load foobar.mat.
> 
> What can I do to change this?
> 
> Christian

You could do func2str(f) and save the function as a string. Then reconvert it using eval when loading it back in from the .mat file

y = 1;
f = @(x)(x+y);
fstr=func2str(f)
save foobar fstr;


Then later


load foobar fstr;
f=eval(fstr);