Zynk
Zynk

Reputation: 95

String to variable name MATLAB

If for instance I have a variable xa=2, and then I construct a string by joining 'x' and 'a', how can I make this new string have the value 2?

xa=2;
var=strcat('x','a');

The result of this is var=xa, but what I want is var=2.

Thank you

Upvotes: 0

Views: 6415

Answers (1)

Yellows
Yellows

Reputation: 723

Use eval():

var = eval(strcat('x','a'));

It will "evaluate" the string 'xa' and translate it to the value of the variable xa.

Source : MATLAB documentation

Upvotes: 4

Related Questions