What would you do?

1 Ansicht (letzte 30 Tage)
Kye Taylor
Kye Taylor am 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])
or
fHandle = eval(['@(t,x)',rhs]);
or
?
Can you explain why?

Akzeptierte Antwort

Chad Gilbert
Chad Gilbert am 3 Jul. 2013
I'd tend to choose str2func, merely because it makes it more obvious what I'm anticipating as an output.
  1 Kommentar
Kye Taylor
Kye Taylor am 8 Jul. 2013
Good thought. Thanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Shashank Prasanna
Shashank Prasanna am 3 Jul. 2013
Don't use eval - EVER!
There is a whole documentation page that should answer 'why?'
  1 Kommentar
Kye Taylor
Kye Taylor am 8 Jul. 2013
Thanks for the pointer!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Platform and License finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by