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?

 採用された回答

Chad Gilbert
Chad Gilbert 2013 年 7 月 3 日

1 投票

I'd tend to choose str2func, merely because it makes it more obvious what I'm anticipating as an output.

1 件のコメント

Kye Taylor
Kye Taylor 2013 年 7 月 8 日
Good thought. Thanks.

サインインしてコメントする。

その他の回答 (1 件)

Shashank Prasanna
Shashank Prasanna 2013 年 7 月 3 日

1 投票

Don't use eval - EVER!
There is a whole documentation page that should answer 'why?'

1 件のコメント

Kye Taylor
Kye Taylor 2013 年 7 月 8 日
Thanks for the pointer!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Variables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by