Create variables with names from strings
Solution 1
You can do it with eval but you really should not
eval(['x', num2str(i), ' = ', num2str(i)]); %//Not recommended
Rather use a cell array:
x{i} = i
Solution 2
I also strongly advise using a cell array or a struct for such cases. I think it will even give you some performance boost.
If you really need to do so Dan told how to. But I would also like to point to the genvarname
function. It will make sure your string is a valid variable name.
EDIT: genvarname is part of core matlab and not of the statistics toolbox
Solution 3
for k=1:10
assignin('base', ['x' num2str(k)], k)
end
Solution 4
Although it is long overdue, i justed wanted to add another answer.
the function genvarname is exactly for these cases
and if you use it with a tmp structure array you do not need the eval cmd
the example 4 from this link is how to do it http://www.mathworks.co.uk/help/matlab/ref/genvarname.html
for k = 1:5
t = clock;
pause(uint8(rand * 10));
v = genvarname('time_elapsed', who);
eval([v ' = etime(clock,t)'])
end
all the best
eyal
Solution 5
If anyone else is interested, the correct syntax from Dan's answer would be:
eval(['x', num2str(i), ' = ', num2str(i)]);
My question already contained the wrong syntax, so it's my fault.
Potaito
Updated on February 27, 2020Comments
-
Potaito about 4 years
Let's assume that I want to create 10 variables which would look like this:
x1 = 1; x2 = 2; x3 = 3; x4 = 4; . . xi = i;
This is a simplified version of what I'm intending to do. Basically I just want so save code lines by creating these variables in an automated way. Is there the possibility to construct a variable name in Matlab? The pattern in my example would be
["x", num2str(i)]
. But I cant find a way to create a variable with that name.-
Amro about 11 yearsSee MATLAB FAQ: How can I create variables A1, A2,...,A10 in a loop?
-
-
Potaito about 11 yearsWow thanks, very simple. And yes, I will use cells but also this method you presented here ;)
-
Potaito about 11 yearsThanks. I will use arrays instead of cells. The example in my question was just very simple in order to have an easy question for the information I'm looking for. What I'm actually doing isn't as naive as my question :p
-
Dan about 11 yearsIn future, you can actually edit my answer if there are small mistakes like this. There is an edit link at the bottom. If you don't have enough reputation you can also just leave a comment and someone else will make the edit for you. I've fixed mine now ;)
-
Amro about 11 years@bdecaf: btw
genvarname
is part of core MATLAB -
Oleg about 11 yearsI really discourage you from using eval to pop incremental variables in your workspace. If you want a name create a structure:
s.(sprintf('x%d', i)) = i;
-
bdecaf about 11 yearsoh my bad. I could have sworn it was part of that toolbox.
-
Flyto over 9 yearssuggest changing 'base' to 'caller'. That way, AIUI, it ought to work anywhere where somebody is not paying specific attention to workspaces.
-
Charlie Parker about 8 yearswhy is this not recommended?
-
Dan about 8 years@CharlieParker It makes your code hard to read (just compare my two solutions) and forces you to use
eval
again to iterate through your variables which is unnecessarily complicated. It makes your code very difficult to debug and adds in more room for error. Think about how you would find the largestx
in the two solutions as an example of how complicated it's going to be if you useeval
! Also it clutters your workspace with all those variables which you don't need. This is what arrays exist for! It is a sloppy, lazy way yo code that shows you didn't take the time to plan properly. -
pattivacek almost 8 years@CharlieParker eval is also fairly slow because the optimizer can't know what it's going to do.
-
Dan almost 8 years@patrickvacek that's a great point. Do you have a link to a test demonstrating this maybe?
-
pattivacek almost 8 years@Dan not readily available, but the idea is straight from the source: mathworks.com/help/matlab/matlab_prog/…