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:
Function handles versus strings

Subject: Function handles versus strings

From: Richard Crozier

Date: 4 Mar, 2010 10:32:20

Message: 1 of 3


I'm writing an optimization routine which accepts a the name of a matlab function to be minimized and uses feval to evaluate that function. My question is, is there a major benefit to using function handles over strings in the input of feval? In general is it better to use function handles than just passing the name of the function as a string to feval?

Thanks,
Richard

Subject: Function handles versus strings

From: Loren Shure

Date: 8 Mar, 2010 14:03:26

Message: 2 of 3

In article <hmo27k$lgt$1@fred.mathworks.com>, r.crozier@ed.ac.uk says...
>
> I'm writing an optimization routine which accepts a the name of a matlab function to be minimized and uses feval to evaluate that function. My question is, is there a major benefit to using function handles over strings in the input of feval? In general is it better to use function handles than just passing the name of the function as a string to feval?
>
> Thanks,
> Richard
>


Yes. If the function in question is going to be evaluated many times,
as can happen in an optimization, function handles should be faster
because the function in question doesn't need to be resolved on the path
each time it's executed.


--
Loren
http://blogs.mathworks.com/loren
http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Function handles versus strings

From: Steven Lord

Date: 8 Mar, 2010 15:32:32

Message: 3 of 3


"Loren Shure" <loren.shure@mathworks.com> wrote in message
news:MPG.25fecdbc84a56506989ac0@news.mathworks.com...
> In article <hmo27k$lgt$1@fred.mathworks.com>, r.crozier@ed.ac.uk says...
>>
>> I'm writing an optimization routine which accepts a the name of a matlab
>> function to be minimized and uses feval to evaluate that function. My
>> question is, is there a major benefit to using function handles over
>> strings in the input of feval? In general is it better to use function
>> handles than just passing the name of the function as a string to feval?
>>
>> Thanks,
>> Richard
>>
>
>
> Yes. If the function in question is going to be evaluated many times,
> as can happen in an optimization, function handles should be faster
> because the function in question doesn't need to be resolved on the path
> each time it's executed.

In addition to Loren's comment, if you're planning to deploy your function,
using function handles instead of strings will help you avoid some potential
problems.

http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/br2cqa0-2.html#br2cqa0-5

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

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