MATLAB: String to Variable conversion

type conversion

So first I declare some variables:
data.variable=12
v1='variable'
n=1
The results:
strcat('data.', v1)
data.variable
But
strcat('data.', (strcat('v',num2str(n))))
data.v1
Instead of data.variable. I'm assuming that this is because v1 is a string. What should I do to make the second command return data.variable?

Best Answer

You might find dynamic field names useful. Doc says:
Generate Field Names from Variables
[...]
For example, create a field name from the current date:
currentDate = datestr(now,'mmmdd');
myStruct.(currentDate) = [1,2,3]