How can I use the value of a variable stored in a cell as a string without using the eval function?

Hey folks, I am currently working on a project and I need your help ! Here is what I wanna do : I’m trying to implement an optimisation process. The names of the variables are saved in a cell containing strings.
For example : test={‘a’,’b’,’c’} with a, b and c the names of the variables
The value of these variables changes at each iteration.
For one iteration for example : Variable(1)=10 Variable(2)=20 Variable(3)=30
I want to assign these values to the corresponding variable in order to run the calculation with the new values of the variables contained in test so I’m doing :
Assignin(‘base’,test{1},variable(1))
Hence a=10 Everything is working perfectly so far. The calculation runs and works but at a certain point, I need to know the value of the variable.
If I execute test{1}, I get ‘a’.
The only way I find to get the value of a is to use the function eval Eval(test{1})=10.
It worked so far but in a few days, I’ll need to compile my Matlab script and I’ve seen that it doesn’t work with eval functions. I would like to find an alternative but I can’t find any.. if some of you have any idea I would be really grateful.
Cheers !

1 Comment

"I would like to find an alternative but I can’t find any"
The alternative is better program design.
Using eval, assignin, and evalin to access variables in a loop are signs of bad code design. They will make your code slow, buggy, hard to read, and hard to debug:
The MATLAB documentation specifically states that eval should be avoided for trivial iterations over variables (which is basically what you are trying to do): "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended"
You are also moving data using the least recommended method of passing data between workspaces: "Use them sparingly."
As the documentation clearly states: "Best Practice: Passing Arguments".
So you picked several methods of writing code that experts on this forum continually advise to avoid, because they make code slow, buggy, and hard to work with... and now you are finding that they are making your code slow, buggy, and hard to work with.
What kind of advice do you want to hear?
The best advice you will get is to write your code without using eval, assignin, etc, by instead using the fast, efficient, reliable, and simple practices of passing data as arguments, using indices, etc. Then not only will you learn how to write much better code, it will be simpler to make it compile.

Sign in to comment.

Answers (2)

Do not use 3 distince variables but a single vector:
param = [10, 20, 30];
Then param(1) is your a and the access is easy and efficient.
Even the assignin is something I would avoid strictly. Better store the variables in a GUI (UserData, ApplicationData, guidata) and start the computations directly from there and not from the command window.
Thank you Stephen, thank you Jan !
I am currently trying to delete all my global variables. I have created a structure and my variables are stored within. I can use my variables which are read at the beginning of the process by putting the structure in the argument of the function I want to run.
But I don't know if I may be able to solve my problem. Actually my input file is a .csv file and I wanna create my Matlab code so I just need to modify my csv file !
I want to be able to change the variables I want to optimise so it is stored like this in a cell as strings :
name_variable={'variable1toOptimized','variable2toOptimized'}
During the calculation, I want to assign values to these variables (the value changes at each iteration). So I have another process from which I obtain for example :
v1=10 and v2=15.
Now I want to assign the value of v1 and v2 to the variable corresponding to the strings stored in name_variable.
It works perfectly by doing assignin('base',v1,nom_variable{1,1}) but I want to avoid that...
To sum up, name_variable is a variable containing strings that correspond to names of variables I wanna optimize.
I have variables called variable1toOptimized and variable2toOptimized and i want them to be assigned the value of v1 and v2 to run the calculation. But I may want to optimize variable3toOptimized so I would have
name_variable={'variable1toOptimized','variable3toOptimized'} for example.
And once the values are assigned I want to run the calculation using the variable with its new value.
That was easy using global variables because I assigned the value in the base workspace and I used evalin base. But now I want to avoid the use of assignin and eval and I have no idea how to do it...

Categories

Asked:

on 26 Apr 2017

Commented:

on 27 Apr 2017

Community Treasure Hunt

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

Start Hunting!