Why eval() and evalin() are not recommended?
7 ビュー (過去 30 日間)
古いコメントを表示
I have read so many times that functions eval() and evalin() are not recommended. What is the reason?
I have a situation where i want to evaluate the value of a parameter whose name is stored in string.
I obtained parameter name as
CostName=get_param(cblk,'Name');
Now i want value of this parameter from workspace so i wrote
CostVal=eval(strcat(CostName,'.Value'));
Is there any alternative for above line instead of using eval();
0 件のコメント
採用された回答
Daniel Shub
2011 年 9 月 22 日
See the FAQ and TMW for reasons to avoid eval and evalin
As your code is written now, you probably cannot avoid eval. One possible work around would be if all of your "parameters" (i.e., anything that could be returned by get_param) were saved in a structure. Then you could do
param.(CostName).Value
You really need to show us how the variable that the string in CostName points to was created in order for us to give you ideas about how to avoid eval.
7 件のコメント
K E
2012 年 4 月 3 日
Simulink coding seems to rely on many practices that Matlab experts recommend against, such as frequent use of eval and evalin; or populating the workspace with many variables without tracking where they came from. See #6, 7 and 9 on http://blogs.mathworks.com/loren/2012/01/13/best-practices-for-programming-matlab/ . Perhaps these two departments of Mathworks could benefit from talking more often.
その他の回答 (0 件)
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!