Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: easy function handles questions

Subject: easy function handles questions

From: matt dash

Date: 28 Feb, 2008 18:23:13

Message: 1 of 5

Function handles have always been a mystery to me, so I have
two easy questions:

I have an optimization function where the name of the
objective function is stored as a string, and I call it like
this:

objfun='myfunctionname'

eval(['objectivevalues=',objfun,'(inputs);']);

Is there a way I can get rid of the eval (I'm assuming this
requires a function handle)?


Also, I have a gui that (among other things) allows the user
to paste in a function from another program, and it converts
it to a matlab function. Right now I save the resulting
function as an m-file, but since the functions are only used
in the current session of that gui, it would be nice to just
store the function in the workspace instead of actually
writing it to a file. Is this possible for arbitrarily large
functions? (A typical function might be 10 lines long and
contain if statements or for loops, but they are always
single input, single output.) I know you can do this using
anonymous function handles for short functions, but I'm
wondering if it's possible for more complex ones. (Assume
the function would be stored in a string array.)

Thanks,
Matt



Subject: easy function handles questions

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 28 Feb, 2008 18:37:19

Message: 2 of 5

In article <fq6u6h$rgb$1@fred.mathworks.com>,
matt dash <md222@mail.gatech.edu> wrote:

>Also, I have a gui that (among other things) allows the user
>to paste in a function from another program, and it converts
>it to a matlab function. Right now I save the resulting
>function as an m-file, but since the functions are only used
>in the current session of that gui, it would be nice to just
>store the function in the workspace instead of actually
>writing it to a file. Is this possible for arbitrarily large
>functions? (A typical function might be 10 lines long and
>contain if statements or for loops, but they are always
>single input, single output.)

Sorry, anonymous functions are always a single expression, no
if or loops permitted. I believe that Steve Lord showed a way
in which you could create the ternary operator using anonymous
function handles, which would allow for if/else in anonymous
functions (but not in any nice syntax); for loops in anonymous
functions would pretty much require using "continuation" style
which is not particularily fun to write and get right.
--
  "To the modern spirt nothing is, or can be rightly known,
  except relatively and under conditions." -- Walter Pater

Subject: easy function handles questions

From: Peter Boettcher

Date: 28 Feb, 2008 18:48:35

Message: 3 of 5

"matt dash" <md222@mail.gatech.edu> writes:

> Function handles have always been a mystery to me, so I have
> two easy questions:
>
> I have an optimization function where the name of the
> objective function is stored as a string, and I call it like
> this:
>
> objfun='myfunctionname'
>
> eval(['objectivevalues=',objfun,'(inputs);']);
>
> Is there a way I can get rid of the eval (I'm assuming this
> requires a function handle)?

feval(objfun, inputs) would be better. Then, convert to a
handle if you want:

myfunctionhandle = str2func(objfun); % function name stored a string
or
myfunctionhandle = @myfunctionname; % literal function name

feval(myfunctionhandle, inputs);


-Peter

Subject: easy function handles questions

From: Steven Lord

Date: 28 Feb, 2008 20:28:06

Message: 4 of 5


"matt dash" <md222@mail.gatech.edu> wrote in message
news:fq6u6h$rgb$1@fred.mathworks.com...
> Function handles have always been a mystery to me, so I have
> two easy questions:
>
> I have an optimization function where the name of the
> objective function is stored as a string, and I call it like
> this:
>
> objfun='myfunctionname'
>
> eval(['objectivevalues=',objfun,'(inputs);']);
>
> Is there a way I can get rid of the eval (I'm assuming this
> requires a function handle)?

Yes. Look at the function STR2FUNC, and these two postings from Loren
Shure's blog about FEVAL and function handles:

http://blogs.mathworks.com/loren/2006/01/11/mental-model-for-feval/

http://blogs.mathworks.com/loren/2006/11/17/some-ways-to-create-function-handles/

In this particular case, you'd have:

objfun = 'myfunctionname';
objhandle = str2func(objfun);
objectivevalues = objhandle(inputs); % Assuming you're using Release 14 or
later

> Also, I have a gui that (among other things) allows the user
> to paste in a function from another program, and it converts
> it to a matlab function. Right now I save the resulting
> function as an m-file, but since the functions are only used
> in the current session of that gui, it would be nice to just
> store the function in the workspace instead of actually
> writing it to a file. Is this possible for arbitrarily large
> functions? (A typical function might be 10 lines long and
> contain if statements or for loops, but they are always
> single input, single output.) I know you can do this using
> anonymous function handles for short functions, but I'm
> wondering if it's possible for more complex ones. (Assume
> the function would be stored in a string array.)

No, we don't provide any functionality to do that. It's an interesting
idea, though.

--
Steve Lord
slord@mathworks.com


Subject: easy function handles questions

From: matt dash

Date: 29 Feb, 2008 17:16:01

Message: 5 of 5

Thanks for the replies guys. STR2FUNC is particularly helpful.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics