MATLAB Answers

How to rename a variable with a char

32 views (last 30 days)
Peter Knowles
Peter Knowles on 20 Sep 2018
Commented: Peter Knowles on 21 Sep 2018
In my work space i have a variable that i'd like to rename, with a name that is held as a char.
I want to do this in a simple m file which I can run whenever required.
I have some code that works
%where label name is my newname and DataCopy is my variable
str = [Label_name,'= DataCopy;'];
eval(str);
But I keep reading not to use the eval command, i don't know why and frankly i don't care. What i do know is it works ok from the command but crashes when run in my program.
I would just like to find a way of doing this simple repeated task.
I've tried a few other options but just seem to end up putting the string into the data and not renaming!
  9 Comments
Peter Knowles
Peter Knowles on 21 Sep 2018
Thank you this works. It's what i was hoping for two easy lines of code that fit into the existing program without breaking anything. Nice :-)

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 20 Sep 2018
This bad code works either from the command line or in a script, though no competent MATLAB programmer in the world would recommend it (not even me):
Label_name = input('Enter the variable name : ', 's')
DataCopy = rand(2)
str = [Label_name,'= DataCopy;']
eval(str)
% At this point we have no idea what the variable is called,
% unless we pull some tricks with whos()
% which I'm not going to go into.
% But we'll assume you typed in "unwise"
% so let's look as the properties of a variable called unwise.
whos unwise
unwise
% IMPORTANT NOTE: Like Jos said, if you don't know what the string is,
% then how will you use it later in your code?
  2 Comments
Guillaume
Guillaume on 20 Sep 2018
quicker than getting a straight answer from here!
You're complaining about getting sarcam yet your comment and question appear very antagonistic.
You've had a very straight answer by Image Analyst. The answer he's written works and is the simplest way to rename the variable using eval. So, if it doesn't work for you, that's because either you're doing it wrong, or what you're trying to achieve is not what you've described. Either way, you can't complain that you're not getting the right answer if you're not describing the problem properly.
and it crashed
What does that mean? Crash normally means that matlab terminates and thus that you've encountered a serious bug. Perhaps, you mean it errors (completely different meaning!) in which case, you're not helping us by not giving us the error message.
Obviously renaming the 50 odd arrays manually would be quicker
Even faster would be to not rename the variables at all and continue using the names they already have. You say you don't want to discuss this, but this is the approach we would naturally take. Perhaps, your use case is special, but if you don't explain it, we can't understand what needs doing.
Disclaimer: No sarcasm was intended in this post.

Sign in to comment.


OCDER
OCDER on 20 Sep 2018
"the output is a struct which contains fields and values."
S = otherPersonFunction(...); %Returns structure with fields and values
"I want each of these arrays saved into a mat file" Do you want it to be saved as "NewArray"?
Fields = fieldnames(S);
for f = 1:length(Fields)
NewArray = S.(Fields{f});
save(sprintf('%d.mat', f), 'NewArray'); %You'll generate 1.mat, 2.mat, etc each storing a NewArray variable
end
"Each array is then used in a matlab simulink model."
Temp = load('l.mat', 'NewArray');
NewArray = Temp.NewArray;
or just
load('1.mat') %will poof NewArray into the workspace - not recommended

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by