How can I use the value of a variable stored in a cell as a string without using the eval function?
Show older comments
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.
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.
Simon B
on 27 Apr 2017
0 votes
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!