No products are associated with this question.
Use eval. For example
xxx = 3; % variable with value varname = 'xxx'; % name of variable stored as a string
% ... other stuff ...
val_of_xxx = eval(varname); % gets back value of xxx
But think carefully about whether you really need to do this. It's often the case that there's a better way to write your program, perhaps using structure arrays or cell arrays, that avoids having to store variable names as strings. If there is, it's usually much better to avoid using eval.
Building on David's answer
xxx = 3; varname = 'xxx'; val_of_xxx = eval(varname);
can be replaced with
data.xxx = 3; varname = 'xxx'; val_of_xxx = data.(varname);
The advantage of the latter is it avoids the eval.
Play games and win prizes!Learn more