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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!