Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Why does everyone hate 'eval'?
Date: Mon, 15 Dec 2008 16:46:42 +0000 (UTC)
Organization: Universit&#228;tsSpital Z&#252;rich
Lines: 25
Message-ID: <gi61lh$2gh$1@fred.mathworks.com>
References: <ghrlim$oqo$1@fred.mathworks.com> <ghv5ut$fj7$1@fred.mathworks.com> <gi5ujh$b3d$1@fred.mathworks.com>
Reply-To: <HIDDEN>
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 1229359602 2577 172.30.248.37 (15 Dec 2008 16:46:42 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 15 Dec 2008 16:46:42 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 11
Xref: news.mathworks.com comp.soft-sys.matlab:507123

"Steven Lord"
> One use case that I haven't been able to 
> think of a way to handle without using EVAL is converting a user-input 
> string into an anonymous function.

> function y = stringToAnonymous(str, vars)
> y = eval(['@(' vars ') ' str]);

well,...

% the command string
     var='a';
     str='cosd(a)';
     com=sprintf('@(%s) %s',var,str);
% the engine
     tmp=inline(com);
     fh=tmp('anything');
     whos fh
%    fh        1x1                16  function_handle
% the application
     fh(60)
%    ans = 0.5

note: i never said it was beautiful, i just said it's possible... - and - as was discussed several times in the past: we all hope that STR2FUNC eventually will handle this...
us