## How to make dynamic variable names (A1, A2, ..., An) with "for" loop? Using "eval" "num2str"?

### Italo (view profile)

on 13 Nov 2013
Latest activity Answered by Stephen Cobeldick

on 12 Jan 2016

### Stephen Cobeldick (view profile)

Hello community,

my knowledge of Matlab is limited, I admit it. So, I am sorry if I am going to make mistakes.

I have to create a series of variable using a "for" loop.

I make this:

--------------------------

```for i=1:3
```
```eval(['A' num2str(i) '= i'])
```
```end
--------------------------
```

and it works well, it makes 3 variables A1, A2, A3.

But I need to use this variables to make other variables B1, B2, B3 where Bi=Ai*i. So I should have B1=A1*1=1, B2=A2*2=2*2=4, B3=A3*3=3*3=9

I tried something like this:

----------------------------

```for i=1:3
```
```eval(['A' num2str(i) '= i'])
eval(['B' num2str(i) '= 'A' num2str(i) '*i])
```
```end
------------------------------
```

but it gives me error and it doesn't work. Of course I simplified things, the real code is a bt more complicated but I need to know the basics of how this could work.

Thank you

Muna

### Muna (view profile)

on 22 Jan 2014
```for i=1:3
genvarname('A',  num2str(i));
eval(['A' num2str(i) '= i'])
genvarname('B',  num2str(i));
eval(['B' num2str(i) '= A' num2str(i) '*i'])
end
```

## Products

No products are associated with this question.

### Stephen Cobeldick (view profile)

on 12 Jan 2016

Ugh... this is a really bad idea!

Avoid creating dynamically named variables in MATLAB. This is poor practice as has been explained many times on this forum, and is not recommended by MATLAB themselves:

http://www.mathworks.com/help/matlab/matlab_prog/string-evaluation.html

When you are a beginner it seems like a cunning and fast way to store information, but actually it is really bad practice to name your variables dynamically. MATLAB is also not intended for this kind of variable naming: if you continue to include data in the variable names then you will find yourself fighting many more of these battles against MATLAB.

However when you use more appropriate storage for your data (and meta-data) then you will suddenly find lots of MATLAB functions that do many useful operations for you, quickly and easily.

In your case a much more robust solution would be to use:

There are many functions that support working on structures and cell arrays and can access their data easily, and many functions operate on complete numeric arrays all at once without any loops (i.e. vectorized code, which is something you need to learn about).

Placing your data in a structure or cell array also makes it much easier to pass to functions: can you imagine the fight you would have trying to pass hundreds of dynamically named variables to a function?

If you have a newer version of matlab you can also use a table , which stores the data together in one array but also allows key-name access to the columns. This might be a good alternative for your data.

In case you are interested, here are some pages explaining why dynamically assigning variable names is a really bad idea in MATLAB:

http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

http://stackoverflow.com/questions/10272522/use-and-implications-of-evalexpression-in-matlab-code

Here is a discussion of why it is a bad idea to include meta-data (such as an index) in a variable name:

And here is a discussion of why it is a really bad idea to make variables magically appear any workspace (even though beginners love doing this):

And in case you thought this is a MATLAB restriction, here is the same discussion for some other languages, advising "DO NOT create dynamic variable names":

http://nedbatchelder.com/blog/201112/keep_data_out_of_your_variable_names.html

http://www.techrepublic.com/blog/it-security/never-use-dynamic-variable-names/

http://stackoverflow.com/questions/11118486/python-list-as-variable-name

http://stackoverflow.com/questions/25076883/creating-dynamically-named-variables-in-a-function-in-python-3-understanding-e

http://perl.plover.com/varvarname.html

https://24ways.org/2005/dont-be-eval

on 22 Jan 2014
Edited by Amit

### Amit (view profile)

on 22 Jan 2014

Italo, I am glad you're trying out things on matlab.

Matlab is very good with matrixes. What you're doing in these can be done much easily. For example:

```A = 1:3;
B = A.*A; %
```

You can find many tutorials online on how to use MAtlab. For example: http://www.mathworks.com/academia/student_center/tutorials/launchpad.html

Amit

### Amit (view profile)

on 22 Jan 2014

In you code:

```for i=1:3
eval(['A' num2str(i) '= i'])
eval(['B' num2str(i) '= A' num2str(i) '*i']) % Issue was Here. Notice the positions of '
end
```
Bruno Pop-Stefanov

### Bruno Pop-Stefanov (view profile)

on 22 Jan 2014

I would like to add that eval is #1 in this list: Top 10 MATLAB code practices that make me cry

#### Join the 15-year community celebration.

Play games and win prizes!

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi