Clear Filters
Clear Filters

What would you do?

4 views (last 30 days)
Kye Taylor
Kye Taylor on 3 Jul 2013
Suppose you have a string like
rhs = 'x*cos(x)/(t+1)';
and that you want to create a function handle... would you do
fHandle = str2func(['@(t,x)',rhs])
fHandle = eval(['@(t,x)',rhs]);
Can you explain why?

Accepted Answer

Chad Gilbert
Chad Gilbert on 3 Jul 2013
I'd tend to choose str2func, merely because it makes it more obvious what I'm anticipating as an output.

More Answers (1)

Shashank Prasanna
Shashank Prasanna on 3 Jul 2013
Don't use eval - EVER!
There is a whole documentation page that should answer 'why?'

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!