Why eval() and evalin() are not recommended?

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();

 Respuesta aceptada

Daniel Shub
Daniel Shub el 22 de Sept. de 2011
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 comentarios

TAB
TAB el 22 de Sept. de 2011
I am using simulink. I created the parameter in base workspace by
SensorStatus=Simulink.Parameter;
I am using this parameter in a constant block in model. Name of const. block is same i.e 'SensorStatus'.
Now i am writing a script to read the const. block name from model using 'get_param()' and evaluate its value from workspace. Finally put all data in text file.
'CostName=get_param(cblk,'Name')' returns the const. block name as string only. It can not be collected in struct.
Daniel Shub
Daniel Shub el 22 de Sept. de 2011
If I follow you correctly, CostName is the string "SensorStatus" and your workspace also has the variable SensorStatus. You want to evaluate the expression
CostVal=SensorStatus.Value
I am not suggesting collection the strings in a struct (although I don't see why you couldn't). Rather, I think the following should work:
param.SensorStatus=Simulink.Parameter;
CostName=get_param(cblk,'Name');
CostVal=param.(CostName).Value;
Jan
Jan el 22 de Sept. de 2011
+1, I agree with Daniel.
After "CostName=get_param(cblk,'Name');" you need an EVAL, e.g. in:
CostVar = eval(CostName); CostVal = CostVar.Value;
But there is no good reason to pass variables by their names.
TAB
TAB el 22 de Sept. de 2011
@Daniel:
You got my question correctly. I tried code you suggested, but it is not working.
I tried following lines on command window
SensorStatus=Simulink.Parameter;
SensorStatus.Value=100;
param.SensorStatus=Simulink.Parameter;
CostName='SensorStatus';
CostVal=param.(CostName).Value;
>> CostVal =
[]
I think here 'param.SensorStatus=Simulink.Parameter' will create a separate structure 'param'.
Daniel Shub
Daniel Shub el 22 de Sept. de 2011
Try this:
param.SensorStatus=Simulink.Parameter;
param.SensorStatus.Value=100;
CostName='SensorStatus';
CostVal=param.(CostName).Value;
TAB
TAB el 26 de Sept. de 2011
I think, there is no alternative of eval() in this case.
K E
K E el 3 de Abr. de 2012
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Variables en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

TAB
el 22 de Sept. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by