<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235926</link>
    <title>MATLAB Central Newsreader - Saving an anonymous function handle to a mat-file</title>
    <description>Feed for thread: Saving an anonymous function handle to a mat-file</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Fri, 12 Sep 2008 12:34:49 -0400</pubDate>
      <title>Saving an anonymous function handle to a mat-file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235926#600061</link>
      <author>Christian Zietz</author>
      <description>Hi,&lt;br&gt;
&lt;br&gt;
when I enter&lt;br&gt;
&lt;br&gt;
y = 1;&lt;br&gt;
f = @(x)(x+y);&lt;br&gt;
save foobar f;&lt;br&gt;
&lt;br&gt;
in the MATLAB command window, the resulting foobar.mat will load and &lt;br&gt;
work when copied to another computer.&lt;br&gt;
&lt;br&gt;
When I execute the same code inside a function and copy foobat.mat, &lt;br&gt;
MATLAB on the other computer needs the m-file containing the function &lt;br&gt;
where I defined the function handle &quot;f&quot; to load foobar.mat.&lt;br&gt;
&lt;br&gt;
What can I do to change this?&lt;br&gt;
&lt;br&gt;
Christian</description>
    </item>
    <item>
      <pubDate>Fri, 12 Sep 2008 16:55:04 -0400</pubDate>
      <title>Re: Saving an anonymous function handle to a mat-file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235926#600116</link>
      <author>Matt </author>
      <description>Christian Zietz &amp;lt;newsgroup@chzsoft.com.ar&amp;gt; wrote in message &amp;lt;6iv5vcFn3i0U1@mid.individual.net&amp;gt;...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; when I enter&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; y = 1;&lt;br&gt;
&amp;gt; f = @(x)(x+y);&lt;br&gt;
&amp;gt; save foobar f;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; in the MATLAB command window, the resulting foobar.mat will load and &lt;br&gt;
&amp;gt; work when copied to another computer.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; When I execute the same code inside a function and copy foobat.mat, &lt;br&gt;
&amp;gt; MATLAB on the other computer needs the m-file containing the function &lt;br&gt;
&amp;gt; where I defined the function handle &quot;f&quot; to load foobar.mat.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; What can I do to change this?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Christian&lt;br&gt;
&lt;br&gt;
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&lt;br&gt;
&lt;br&gt;
y = 1;&lt;br&gt;
f = @(x)(x+y);&lt;br&gt;
fstr=func2str(f)&lt;br&gt;
save foobar fstr;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Then later&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
load foobar fstr;&lt;br&gt;
f=eval(fstr);</description>
    </item>
    <item>
      <pubDate>Fri, 12 Sep 2008 17:10:44 -0400</pubDate>
      <title>Re: Saving an anonymous function handle to a mat-file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235926#600117</link>
      <author>Christian Zietz</author>
      <description>Matt schrieb:&lt;br&gt;
&lt;br&gt;
&amp;gt; You could do func2str(f) and save the function as a string. Then&lt;br&gt;
&amp;gt; reconvert it using eval when loading it back in from the .mat file&lt;br&gt;
&lt;br&gt;
&amp;gt; y = 1;&lt;br&gt;
&amp;gt; f = @(x)(x+y);&lt;br&gt;
&amp;gt; fstr=func2str(f)&lt;br&gt;
&amp;gt; save foobar fstr;&lt;br&gt;
&lt;br&gt;
&amp;gt; Then later&lt;br&gt;
&lt;br&gt;
&amp;gt; load foobar fstr;&lt;br&gt;
&amp;gt; f=eval(fstr);&lt;br&gt;
&lt;br&gt;
This won't work unless I also save all variables involved in the &lt;br&gt;
anonymous function (in this case it's only &quot;y&quot;), because &quot;y&quot; will be &lt;br&gt;
undefined (or even worse: have another value) when I reload foobar.mat &lt;br&gt;
and evaluate &quot;fstr&quot;.&lt;br&gt;
&lt;br&gt;
Christian&lt;br&gt;
-- &lt;br&gt;
Christian Zietz  -  CHZ-Soft  -  czietz (at) gmx.net&lt;br&gt;
WWW: &lt;a href=&quot;http://www.chzsoft.com.ar/&quot;&gt;http://www.chzsoft.com.ar/&lt;/a&gt;&lt;br&gt;
PGP/GnuPG-Key-ID: 0x6DA025CA</description>
    </item>
    <item>
      <pubDate>Fri, 12 Sep 2008 17:15:05 -0400</pubDate>
      <title>Re: Saving an anonymous function handle to a mat-file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235926#600118</link>
      <author>Bruno Luong</author>
      <description>&quot;Matt &quot; &amp;lt;mjacobson.removethis@xorantech.com&amp;gt; wrote in message &amp;lt;gae6t8$7de$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; load foobar fstr;&lt;br&gt;
&amp;gt; f=eval(fstr);&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
But the variable y=1 is lost for ever...&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Fri, 12 Sep 2008 17:39:02 -0400</pubDate>
      <title>Re: Saving an anonymous function handle to a mat-file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235926#600122</link>
      <author>Matt </author>
      <description>&lt;br&gt;
&amp;gt; This won't work unless I also save all variables involved in the &lt;br&gt;
&amp;gt; anonymous function (in this case it's only &quot;y&quot;), because &quot;y&quot; will be &lt;br&gt;
&amp;gt; undefined (or even worse: have another value) when I reload foobar.mat &lt;br&gt;
&amp;gt; and evaluate &quot;fstr&quot;.&lt;br&gt;
&lt;br&gt;
That's right it won't, but you never discussed the role of y in your original post, so I could only assume that it is a parameter that would be made available when the function was loaded back in.&lt;br&gt;
&lt;br&gt;
One approach would be to do&lt;br&gt;
&lt;br&gt;
strrep(fstr,y,numstr(y));&lt;br&gt;
&lt;br&gt;
before saving. &lt;br&gt;
&lt;br&gt;
You could also consider setting up f() as an inline function instead of an anonymous one, in a similar way.</description>
    </item>
    <item>
      <pubDate>Fri, 12 Sep 2008 17:52:01 -0400</pubDate>
      <title>Re: Saving an anonymous function handle to a mat-file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235926#600123</link>
      <author>Matt </author>
      <description>&quot;Matt &quot; &amp;lt;mjacobson.removethis@xorantech.com&amp;gt; wrote in message &amp;lt;gae9fm$r7n$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; This won't work unless I also save all variables involved in the &lt;br&gt;
&amp;gt; &amp;gt; anonymous function (in this case it's only &quot;y&quot;), because &quot;y&quot; will be &lt;br&gt;
&amp;gt; &amp;gt; undefined (or even worse: have another value) when I reload foobar.mat &lt;br&gt;
&amp;gt; &amp;gt; and evaluate &quot;fstr&quot;.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I think I misunderstood you earlier.&lt;br&gt;
&lt;br&gt;
No, when you execute&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt;f=eval(fstr)&lt;br&gt;
&lt;br&gt;
the result is&lt;br&gt;
&lt;br&gt;
f = &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@(x)(x+y)&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
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?</description>
    </item>
    <item>
      <pubDate>Fri, 12 Sep 2008 18:16:01 -0400</pubDate>
      <title>Re: Saving an anonymous function handle to a mat-file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235926#600126</link>
      <author>Bruno Luong</author>
      <description>Christian Zietz &amp;lt;newsgroup@chzsoft.com.ar&amp;gt; wrote in message &amp;lt;6iv5vcFn3i0U1@mid.individual.net&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; What can I do to change this?&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Please try this:&lt;br&gt;
&lt;br&gt;
% file savefun.m&lt;br&gt;
function savefun(file, f)&lt;br&gt;
&lt;br&gt;
sfun = functions(f);&lt;br&gt;
sfun.file = '';&lt;br&gt;
save(file,'sfun');&lt;br&gt;
&lt;br&gt;
% file loadfun.m&lt;br&gt;
function f=loadfun(file)&lt;br&gt;
&lt;br&gt;
data=load(file);&lt;br&gt;
sfun = data.sfun;&lt;br&gt;
&lt;br&gt;
for n=1:length(sfun.workspace)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;wn = sfun.workspace{n};&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;vlist=fieldnames(wn);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for k=1:length(vlist)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;val = wn.(vlist{k}); %#ok&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;eval([vlist{k} '= val;']);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
f=eval(sfun.function);&lt;br&gt;
&lt;br&gt;
% file t.m&lt;br&gt;
function t&lt;br&gt;
&lt;br&gt;
y = 1;&lt;br&gt;
f = @(x)(x+y);&lt;br&gt;
savefun('foo.mat', f);&lt;br&gt;
&lt;br&gt;
% end t&lt;br&gt;
&lt;br&gt;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br&gt;
% Now try this in command line&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; t % save anonymous function inside a function&lt;br&gt;
&amp;gt;&amp;gt; f=loadfun('foo.mat') % load in workspace&lt;br&gt;
&lt;br&gt;
f = &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@(x)(x+y)&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; f(0) % evaluate&lt;br&gt;
&lt;br&gt;
ans =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&lt;br&gt;
&lt;br&gt;
% Bruno</description>
    </item>
    <item>
      <pubDate>Fri, 12 Sep 2008 18:19:55 -0400</pubDate>
      <title>Re: Saving an anonymous function handle to a mat-file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235926#600128</link>
      <author>Christian Zietz</author>
      <description>Matt schrieb:&lt;br&gt;
&lt;br&gt;
&amp;gt; Of course, if you then try to evaluate the anonymous function f(x),&lt;br&gt;
&amp;gt; you will get an error unless you first provide y, but presumably you&lt;br&gt;
&amp;gt; have a way of doing that. Otherwise, why would you want to re-use the&lt;br&gt;
&amp;gt; function on a different machine?&lt;br&gt;
&lt;br&gt;
When you declare a function handle, MATLAB &quot;freezes&quot; the current &lt;br&gt;
workspace and saves it together with the function handle. You can go&lt;br&gt;
&lt;br&gt;
y = 1;&lt;br&gt;
f = @(x)(x+y)&lt;br&gt;
clear y;&lt;br&gt;
&lt;br&gt;
and&lt;br&gt;
&lt;br&gt;
f(3)&lt;br&gt;
&lt;br&gt;
will still evaluate to 4. Even if you now enter&lt;br&gt;
&lt;br&gt;
y = pi;&lt;br&gt;
&lt;br&gt;
f(3) will still be 4.&lt;br&gt;
&lt;br&gt;
When you save a function handle to a mat-file the workspace at the time &lt;br&gt;
the function was defined is also saved. This obviously doesn't happen if &lt;br&gt;
you just save the string that is func2str(f).&lt;br&gt;
&lt;br&gt;
Christian&lt;br&gt;
-- &lt;br&gt;
Christian Zietz  -  CHZ-Soft  -  czietz (at) gmx.net&lt;br&gt;
WWW: &lt;a href=&quot;http://www.chzsoft.com.ar/&quot;&gt;http://www.chzsoft.com.ar/&lt;/a&gt;&lt;br&gt;
PGP/GnuPG-Key-ID: 0x6DA025CA</description>
    </item>
    <item>
      <pubDate>Fri, 12 Sep 2008 18:22:09 -0400</pubDate>
      <title>Re: Saving an anonymous function handle to a mat-file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235926#600129</link>
      <author>Christian Zietz</author>
      <description>I wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; When you declare a function handle, MATLAB &quot;freezes&quot; the current &lt;br&gt;
&amp;gt; workspace and saves it together with the function handle.&lt;br&gt;
&lt;br&gt;
PS: &lt;br&gt;
&amp;lt;&lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f4-70115.html#f4-71621&quot;&gt;http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f4-70115.html#f4-71621&lt;/a&gt;&amp;gt;&lt;br&gt;
&lt;br&gt;
Christian&lt;br&gt;
-- &lt;br&gt;
Christian Zietz  -  CHZ-Soft  -  czietz (at) gmx.net&lt;br&gt;
WWW: &lt;a href=&quot;http://www.chzsoft.com.ar/&quot;&gt;http://www.chzsoft.com.ar/&lt;/a&gt;&lt;br&gt;
PGP/GnuPG-Key-ID: 0x6DA025CA</description>
    </item>
    <item>
      <pubDate>Fri, 12 Sep 2008 18:50:18 -0400</pubDate>
      <title>Re: Saving an anonymous function handle to a mat-file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235926#600132</link>
      <author>Bruno Luong</author>
      <description>This loadfun should be safer:&lt;br&gt;
&lt;br&gt;
function fun___=loadfun(file)&lt;br&gt;
&lt;br&gt;
data=load(file);&lt;br&gt;
sfun___ = data.sfun;&lt;br&gt;
&lt;br&gt;
clear file data;&lt;br&gt;
&lt;br&gt;
evalvar(sfun___);&lt;br&gt;
&lt;br&gt;
fun___ = sfun___.function;&lt;br&gt;
clear sfun___;&lt;br&gt;
fun___=eval(fun___);&lt;br&gt;
&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
% nested function&lt;br&gt;
function evalvar(sfun)&lt;br&gt;
&lt;br&gt;
for n=1:length(sfun.workspace)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;wn = sfun.workspace{n};&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;vlist=fieldnames(wn);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for k=1:length(vlist)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;val = wn.(vlist{k});&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;assignin('caller',vlist{k}, val);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
% Bruno</description>
    </item>
    <item>
      <pubDate>Fri, 12 Sep 2008 18:56:38 -0400</pubDate>
      <title>Re: Saving an anonymous function handle to a mat-file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235926#600135</link>
      <author>Christian Zietz</author>
      <description>Bruno Luong schrieb:&lt;br&gt;
&lt;br&gt;
&amp;gt; This loadfun should be safer:&lt;br&gt;
&lt;br&gt;
Thank you very much! I'll give it a try as soon as I'm back at the &lt;br&gt;
computer where MATLAB is installed.&lt;br&gt;
&lt;br&gt;
Christian&lt;br&gt;
-- &lt;br&gt;
Christian Zietz  -  CHZ-Soft  -  czietz (at) gmx.net&lt;br&gt;
WWW: &lt;a href=&quot;http://www.chzsoft.com.ar/&quot;&gt;http://www.chzsoft.com.ar/&lt;/a&gt;&lt;br&gt;
PGP/GnuPG-Key-ID: 0x6DA025CA</description>
    </item>
    <item>
      <pubDate>Fri, 12 Sep 2008 19:42:02 -0400</pubDate>
      <title>Re: Saving an anonymous function handle to a mat-file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235926#600139</link>
      <author>Matt </author>
      <description>&lt;br&gt;
&amp;gt; PS: &lt;br&gt;
&amp;gt; &amp;lt;&lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f4-70115.html#f4-71621&quot;&gt;http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f4-70115.html#f4-71621&lt;/a&gt;&amp;gt;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Christian&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
OK. good to know...</description>
    </item>
    <item>
      <pubDate>Fri, 12 Sep 2008 20:54:03 -0400</pubDate>
      <title>Re: Saving an anonymous function handle to a mat-file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235926#600149</link>
      <author>Matt </author>
      <description>&lt;br&gt;
&amp;gt; When I execute the same code inside a function and copy foobat.mat, &lt;br&gt;
&amp;gt; MATLAB on the other computer needs the m-file containing the function &lt;br&gt;
&amp;gt; where I defined the function handle &quot;f&quot; to load foobar.mat.&lt;br&gt;
&lt;br&gt;
So why does this happen? If anonymous functions carry their own internal snapshots of the workspace, what would it be trying to recover from the m-file where it was born?</description>
    </item>
    <item>
      <pubDate>Fri, 12 Sep 2008 21:44:02 -0400</pubDate>
      <title>Re: Saving an anonymous function handle to a mat-file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235926#600161</link>
      <author>Bruno Luong</author>
      <description>&quot;Matt &quot; &amp;lt;mjacobson.removethis@xorantech.com&amp;gt; wrote in message &amp;lt;gaektb$glf$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; what would it be trying to recover from the m-file where it was born?&lt;br&gt;
&amp;gt;&lt;br&gt;
&lt;br&gt;
Something such as anonymous nested functions of the local m-file.&lt;br&gt;
&lt;br&gt;
If the mfile is modified/changed/deleted, such information is of course lost together with the m-file that carries the anonymous function.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Fri, 12 Sep 2008 22:13:02 -0400</pubDate>
      <title>Re: Saving an anonymous function handle to a mat-file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235926#600168</link>
      <author>Matt </author>
      <description>&quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;gaenr2$dc7$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Matt &quot; &amp;lt;mjacobson.removethis@xorantech.com&amp;gt; wrote in message &amp;lt;gaektb$glf$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; what would it be trying to recover from the m-file where it was born?&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Something such as anonymous nested functions of the local m-file.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If the mfile is modified/changed/deleted, such information is of course lost together with the m-file that carries the anonymous function.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno&lt;br&gt;
&lt;br&gt;
That makes sense, but that surely doesn't explain why  something as simple as @(x) (x+y) is a problem.</description>
    </item>
  </channel>
</rss>

