1

I have a variable called 'int' with alot of data in it. I would like to find a way to programically rename this variable with a user input. So I can query the user indentifcation information about the data, say the response is 'AA1', I want either rename the variable 'int' to 'AA1' or make 'AA1' a variable that is identical to int.

A problem using the input command arises because it allows the user to assign a value to an already created varialbe, instead of actually creating a variable name. Would using the eval function, or a variation of it, help me achieve this? Or is there an easier way?

Thanks!

3
  • Why would you let the user rename a variable? It's usually not a good idea to let a user influence on the underlying implementation of the program. Why not simply associate this data with a character string specified by the user? Commented Jul 18, 2013 at 15:00
  • Why the variable is called 'int', and why do you want to rename? is it an already created variable by somebody else? Commented Jul 18, 2013 at 15:04
  • I have three variables, "vert" "int" and "time", standing for vertical, intensity, and time. I want to place all of these into a stucture, whose name is dependent upon the user. This is for an AFM system and the user is going to input the name of the cantilever being tested. The user is going to test multiple cantilevers, and I want a way to document what data goes with each cantilever. Thus renaming, or creating another varialbe -- or strucutre --- that has its name as an identification tool. Commented Jul 18, 2013 at 17:30

1 Answer 1

2

Just for the record, int is a rather poor variable name choice.

That aside, you can do what you want as follows

say foo is the variable that holds a string that the user input. You can do the following:

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

Assuming int (and now foo) is a structure with field named bar, you can read bar as follows:

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

This is all somewhat clunky.

An alternative approach, that is far less clunky is to use an associative array, and let the user store various values of int in the array. The Matlab approach for associative arrays is Maps. That would be my preferred approach to this problem. Here is an example using the same variables as above.

nameValueMap = containers.Map;
nameValueMap(foo) = int;

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

To get at the data, you just do the following:

intValue = nameValueMap(foo);
Sign up to request clarification or add additional context in comments.

2 Comments

I have three variables, "vert" "int" and "time", standing for vertical, intensity, and time. I want to place all of these into a stucture, whose name is dependent upon the user. This is for an AFM system and the user is going to input the name of the cantilever being tested. The user is going to test multiple cantilevers, and I want a way to document what data goes with each cantilever. Thus renaming, or creating another varialbe -- or strucutre --- that has its name as an identification tool.
@GregDimock, yeah, then you really should go with the associative array. I've added an explicit example above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.