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.)
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
> 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
"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:
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.
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.