View Single Post
badali
#4
Nov18-08, 10:46 AM
P: 1
Matlab: Use string variable as variable name

Hello,

An alternative solution to the one previously posted ( using the assignin function ) is to use the eval function to execute code which is partially specified in a string.

For example,

myvarname = 'f_x';
eval([sprintf(myvarname) '=[10 20 30]']);

Will create a variable called f_x in the workspace with values 10 20 30. Similarly you could have had the array values specified by another variable in the workspace too.

myvarname = 'f_x';
datavalues = [ 5 5 3 ];
eval( [ sprintf(myvarname) '=datavalues+1'] ); % Some arbitrary operation on datavalues

This isn't really better than using assignin but it's a bit more general and my be more suited to your problem.

Thanks