I want to do the following in MATLAB:
dang = 1000;
jam = 'dang';
groo = str2var(jam);
This should give me groo = 1000. How do I do this?
Reason: I have a workspace that contains large integer array variables named like this: var1, var2, ..., var4500. I want to be able to use an iterative method to generate the variable names and the only way I can think of doing this is by concatenating strings in a for loop ('var' 'number'), and using those strings as variable names.
evalfor that. But it's bad practice. As it is having separatte variablesvar1,var2etc. Use a numeric array or a cell array insteadfor ii=1:10 ; MyStruct.(['var' num2str(ii)]) ; end, but you can build the string the way you like. Look at the Matlab doc generate-field-names-from-variables. Your question has already been asked though, you can lookup the different answers here