Got Questions? Get Answers.
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:
dynamic anonymous function

Subject: dynamic anonymous function

From: Balachander Narasimhan

Date: 9 Mar, 2012 01:40:42

Message: 1 of 3

Hi,

I know if we define fn = @(x)x*x, then fn(a) will give the squared value of a

Now suppose I don't know the anonymous function x*x in advance, rather it is obtained out of a string manipulation operation and I have it in 'fnstr' which has 'x' in it

I tried doing 'fn = @(x)fnstr' but it does not work.

Kindly let me know how to do this

Subject: dynamic anonymous function

From: Balachander Narasimhan

Date: 9 Mar, 2012 03:32:42

Message: 2 of 3

Hi,

I was able to fix the problem this way

fn = @(x)eval(fnstr)

regards


"Balachander Narasimhan" <balxxxxxchand@whatever.com> wrote in message <jjbn2q$hj$1@newscl01ah.mathworks.com>...
> Hi,
>
> I know if we define fn = @(x)x*x, then fn(a) will give the squared value of a
>
> Now suppose I don't know the anonymous function x*x in advance, rather it is obtained out of a string manipulation operation and I have it in 'fnstr' which has 'x' in it
>
> I tried doing 'fn = @(x)fnstr' but it does not work.
>
> Kindly let me know how to do this

Subject: dynamic anonymous function

From: Steven_Lord

Date: 9 Mar, 2012 14:28:52

Message: 3 of 3



"Balachander Narasimhan" <balxxxxxchand@whatever.com> wrote in message
news:jjbn2q$hj$1@newscl01ah.mathworks.com...
> Hi,
>
> I know if we define fn = @(x)x*x, then fn(a) will give the squared value
> of a
>
> Now suppose I don't know the anonymous function x*x in advance, rather it
> is obtained out of a string manipulation operation and I have it in
> 'fnstr' which has 'x' in it
>
> I tried doing 'fn = @(x)fnstr' but it does not work.

If you're using release R2009a or later, use STR2FUNC.

http://www.mathworks.com/help/techdoc/rn/bry1ecg-1.html#bry2cut-1

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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