<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165961</link>
    <title>MATLAB Central Newsreader - Anonymous function creation from string</title>
    <description>Feed for thread: Anonymous function creation from string</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>Wed, 19 Mar 2008 12:37:02 -0400</pubDate>
      <title>Anonymous function creation from string</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165961#421690</link>
      <author>Nick Denman</author>
      <description>Hi&lt;br&gt;
&lt;br&gt;
I have a GUI where the user can enter a function (as a&lt;br&gt;
string) that is evaluated in another routine. For example,&lt;br&gt;
the user may enter 'theta(1)*x^2 - theta(2)*x^3'.&lt;br&gt;
&lt;br&gt;
Currently I convert the above string expression to an&lt;br&gt;
anonymous function by using EVAL, e.g. &lt;br&gt;
&amp;gt;&amp;gt; fHnd = eval(['@(x,theta) (',str,')'])&lt;br&gt;
where str is the string given in the GUI. Can anyone suggest&lt;br&gt;
a better way of creating the anonymous function without&lt;br&gt;
using eval?&lt;br&gt;
&lt;br&gt;
Cheers,&lt;br&gt;
Nick</description>
    </item>
    <item>
      <pubDate>Wed, 19 Mar 2008 12:49:01 -0400</pubDate>
      <title>Re: Anonymous function creation from string</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165961#421691</link>
      <author>Christophe Boudrie</author>
      <description>Hi,&lt;br&gt;
&lt;br&gt;
You can directly evaluate the user string as far as all &lt;br&gt;
inputs (&quot;theta&quot;, &quot;x&quot;...) are in your current workspace...&lt;br&gt;
&lt;br&gt;
For instance,&lt;br&gt;
theta=[2 3];&lt;br&gt;
a=1;&lt;br&gt;
&lt;br&gt;
try&lt;br&gt;
&amp;nbsp;&amp;nbsp;my_result=eval('theta(1)*x^2 - theta(2)*x^3');&lt;br&gt;
catch&lt;br&gt;
&amp;nbsp;&amp;nbsp;disp('Unable to evaluate user expression')&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
If the user expression is a simple matlab function, you can &lt;br&gt;
then use &quot;str2func&quot; function.&lt;br&gt;
&lt;br&gt;
regards, chris&lt;br&gt;
&lt;br&gt;
&quot;Nick Denman&quot; &amp;lt;ngdenmanNOSPAM@gmail.REMOVETHIS.com&amp;gt; wrote &lt;br&gt;
in message &amp;lt;frr1de$7a5$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have a GUI where the user can enter a function (as a&lt;br&gt;
&amp;gt; string) that is evaluated in another routine. For example,&lt;br&gt;
&amp;gt; the user may enter 'theta(1)*x^2 - theta(2)*x^3'.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Currently I convert the above string expression to an&lt;br&gt;
&amp;gt; anonymous function by using EVAL, e.g. &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; fHnd = eval(['@(x,theta) (',str,')'])&lt;br&gt;
&amp;gt; where str is the string given in the GUI. Can anyone &lt;br&gt;
suggest&lt;br&gt;
&amp;gt; a better way of creating the anonymous function without&lt;br&gt;
&amp;gt; using eval?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Cheers,&lt;br&gt;
&amp;gt; Nick</description>
    </item>
    <item>
      <pubDate>Thu, 20 Mar 2008 13:38:37 -0400</pubDate>
      <title>Re: Anonymous function creation from string</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165961#421938</link>
      <author>Loren Shure</author>
      <description>In article &amp;lt;frr1de$7a5$1@fred.mathworks.com&amp;gt;, &lt;br&gt;
ngdenmanNOSPAM@gmail.REMOVETHIS.com says...&lt;br&gt;
&amp;gt; Hi&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have a GUI where the user can enter a function (as a&lt;br&gt;
&amp;gt; string) that is evaluated in another routine. For example,&lt;br&gt;
&amp;gt; the user may enter 'theta(1)*x^2 - theta(2)*x^3'.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Currently I convert the above string expression to an&lt;br&gt;
&amp;gt; anonymous function by using EVAL, e.g. &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; fHnd = eval(['@(x,theta) (',str,')'])&lt;br&gt;
&amp;gt; where str is the string given in the GUI. Can anyone suggest&lt;br&gt;
&amp;gt; a better way of creating the anonymous function without&lt;br&gt;
&amp;gt; using eval?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Cheers,&lt;br&gt;
&amp;gt; Nick&lt;br&gt;
&amp;gt; &lt;br&gt;
not sure it will work in your case, but look into str2func.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Loren&lt;br&gt;
&lt;a href=&quot;http://blogs.mathworks.com/loren/&quot;&gt;http://blogs.mathworks.com/loren/&lt;/a&gt;</description>
    </item>
  </channel>
</rss>

