Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
string inputs for anonymous function

Subject: string inputs for anonymous function

From: lambek

Date: 22 Feb, 2009 21:20:18

Message: 1 of 6

Hello, thanks for suggestions or help in advance.

I am writing a code that would allow the user to import equations and variables as strings. Simple example. The user inputs X='a,b,c' and f='a^2+b*c', and the code should create an anonymous function g=@(a,b,c)a^2+b*c. X and f may be different in different instances. I used eval function g=@(a,b,c)eval(f), but there is a problem with the variables in brackets after the @ sign. g=@(eval(X))eval(f) or g=@(X)eval(f) do not work. I cannot get the contents of X as the inputs of the anonymous function, and I do not want the user to mess with any part of the code except the string inputs.

Best,

Lambek

Subject: string inputs for anonymous function

From: Phil Goddard

Date: 23 Feb, 2009 03:27:01

Message: 2 of 6


The following will work:

g = eval(['@(',X,')',f]);

But what if someone enters
>> X ='a,b,c';
>> f = 'exit'; % or something more malicious?
>> g = eval(['@(',X,')',f]);

Phil.

Subject: string inputs for anonymous function

From: Jos

Date: 23 Feb, 2009 10:15:05

Message: 3 of 6

"lambek " <nagvisbunkeri@yahoo.com> wrote in message <gnsfii$38i$1@fred.mathworks.com>...
> Hello, thanks for suggestions or help in advance.
>
> I am writing a code that would allow the user to import equations and variables as strings. Simple example. The user inputs X='a,b,c' and f='a^2+b*c', and the code should create an anonymous function g=@(a,b,c)a^2+b*c. X and f may be different in different instances. I used eval function g=@(a,b,c)eval(f), but there is a problem with the variables in brackets after the @ sign. g=@(eval(X))eval(f) or g=@(X)eval(f) do not work. I cannot get the contents of X as the inputs of the anonymous function, and I do not want the user to mess with any part of the code except the string inputs.
>
> Best,
>
> Lambek

Can't you use an inline function?

f = 'a^2 + b * c' ;
fh = inline(f)
fh(1,2,3)

fh(1:3,2,4)
fh = vectorize(fh)
fh(1:3,2,4)

% with the additional advantage of avoiding eval troubles
f = 'quit' ;
fh2 = inline(f)
fh2(2)

hth
Jos

Subject: string inputs for anonymous function

From: lambek

Date: 23 Feb, 2009 11:40:18

Message: 4 of 6

"Phil Goddard" <philgoddardNOSPAM@telus.net> wrote in message <gnt525$r9f$1@fred.mathworks.com>...
>
> The following will work:
>
> g = eval(['@(',X,')',f]);
>
> But what if someone enters
> >> X ='a,b,c';
> >> f = 'exit'; % or something more malicious?
> >> g = eval(['@(',X,')',f]);
>
> Phil.

Thank you, works for me. I do not worry of someone typing 'exit', it is their problem. I have read some bad staff about eval here :)

Subject: string inputs for anonymous function

From: Mike Karr

Date: 26 Feb, 2009 14:15:48

Message: 5 of 6

Phil Goddard wrote:
> The following will work:
>
> g = eval(['@(',X,')',f]);
>
> But what if someone enters
>>> X ='a,b,c';
>>> f = 'exit'; % or something more malicious?
>>> g = eval(['@(',X,')',f]);

Starting in R2009a, the preferred style is to use:

 > g = str2func(['@(',X,')',f]);

The reason that this is preferable for most purposes is that it does not
pick up
any of the references in the context in which the function handle is
constructed.
This does not matter so much at the prompt, but let's look at what could go
wrong in a .m file. Suppose that you have written the following foo.m:

function h = foo(X,f)
h = str2func(['@(',X,')',f]);

function bar
'hello'

Now, suppose your client, who doesn't know anything about your
subfunction bar
(after all, it is not part of foo.m that is accessible to them), and who
has his/her own
function named bar, types:

 > g = foo('','bar()');

Then when g() is invoked, it will then invoke the user's function named
bar. BUT, if
you change str2func in foo.m to eval, then g() will invoke the
subfunction bar,
which is almost surely undesirable.

It is for reasons like this (in more complicated settings, of course)
that we extended
str2func to anonymous function syntax and insulated the resulting
anonymous function
from the caller's context.

hth,
mike

Subject: string inputs for anonymous function

From: us

Date: 26 Feb, 2009 14:36:01

Message: 6 of 6

Mike Karr
> Starting in R2009a, the preferred style is to use:
> > g = str2func(['@(',X,')',f]);

well, now that this seems to be officially out, we can get rid of the (probably) last evil eval, which some of us could not prevent at the time due to the non-disclosure agreement...

http://www.mathworks.com/matlabcentral/newsreader/create_message?reply_id=617172

:-)
us

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us