How to represent the string value of a variable
Show older comments
I'll start with some example code:
choice = 'mass'; car_mass = car.choice;
The variable "choice" is simply assigned a string value. The variable "car" is a struct that was created earlier in the program, and theoretically in this code "car.choice" should represent "car.mass", which is a double representing the car struct's mass. The problem is, I don't know how to get the statement "car.choice" to instead represent "car.mass". When I run the code right now, it tries to literally evaluate "car.choice" which is obviously not a field in the struct.
Would you use something like "car.(eval(choice))" in hopes that the eval function would return the value of choice, and cause the expression to become "car.mass"?
Please help! Thank you!
Answers (1)
car = struct('mass',1000);
choice = 'mass';
car.(choice)
Categories
Find more on Cell Arrays 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!