Path: news.mathworks.com!not-for-mail
From: "Christophe Boudrie" <christophe.boudrie@altran.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Anonymous function creation from string
Date: Wed, 19 Mar 2008 12:49:01 +0000 (UTC)
Organization: Altran AIT
Lines: 39
Message-ID: <frr23t$dnr$1@fred.mathworks.com>
References: <frr1de$7a5$1@fred.mathworks.com>
Reply-To: "Christophe Boudrie" <christophe.boudrie@altran.com>
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 1205930941 14075 172.30.248.37 (19 Mar 2008 12:49:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 19 Mar 2008 12:49:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1151199
Xref: news.mathworks.com comp.soft-sys.matlab:458030



Hi,

You can directly evaluate the user string as far as all 
inputs ("theta", "x"...) are in your current workspace...

For instance,
theta=[2 3];
a=1;

try
  my_result=eval('theta(1)*x^2 - theta(2)*x^3');
catch
  disp('Unable to evaluate user expression')
end

If the user expression is a simple matlab function, you can 
then use "str2func" function.

regards, chris

"Nick Denman" <ngdenmanNOSPAM@gmail.REMOVETHIS.com> wrote 
in message <frr1de$7a5$1@fred.mathworks.com>...
> Hi
> 
> I have a GUI where the user can enter a function (as a
> string) that is evaluated in another routine. For example,
> the user may enter 'theta(1)*x^2 - theta(2)*x^3'.
> 
> Currently I convert the above string expression to an
> anonymous function by using EVAL, e.g. 
> >> fHnd = eval(['@(x,theta) (',str,')'])
> where str is the string given in the GUI. Can anyone 
suggest
> a better way of creating the anonymous function without
> using eval?
> 
> Cheers,
> Nick