Function handles versus strings

  • Follow


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
0
Reply Richard 3/4/2010 10:32:20 AM

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
0
Reply Loren 3/8/2010 2:03:26 PM


"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 


0
Reply Steven 3/8/2010 3:32:32 PM

2 Replies
254 Views

(page loaded in 0.151 seconds)

Similiar Articles:













7/23/2012 7:05:48 PM


Reply: