MATLAB assign variable name -


i have variable called 'int' alot of data in it. find way programically rename variable user input. can query user indentifcation information data, response 'aa1', want either rename variable 'int' 'aa1' or make 'aa1' variable identical int.

a problem using input command arises because allows user assign value created varialbe, instead of creating variable name. using eval function, or variation of it, me achieve this? or there easier way?

thanks!

just record, int rather poor variable name choice.

that aside, can want follows

say foo variable holds string user input. can following:

% eliminate leading/trailing whitespace foo = strtrim(foo); = regexp('[a-za-z][a-za-z0-9_]*',foo)); if numel(a) == 0      fprintf('sorry, %s not valid variable name in matlab\n', foo); elseif ~= 1     fprintf('sorry, %s not valid variable name in matlab\n', foo); elseif 2 == exist(foo,'var')     fprintf('sorry, %s in use variable name.'); else     eval([foo,' = int']); end 

assuming int (and foo) structure field named bar, can read bar follows:

barval = eval([foo,'.bar']); 

this clunky.

an alternative approach, far less clunky use associative array, , let user store various values of int in array. matlab approach associative arrays maps. preferred approach problem. here example using same variables above.

namevaluemap = containers.map; namevaluemap(foo) = int; 

the above creates association between name stored in foo data in variable int.

to @ data, following:

intvalue = namevaluemap(foo); 

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -