MATLAB: Eval() and evalin() are not recommended

evalevalinMATLAB

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

Best Answer

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.