Asked by Italo
on 13 Nov 2013

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

Answer by Stephen Cobeldick
on 12 Jan 2016

Accepted Answer

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:

- the dimensions of numeric arrays: fully vectorized code is often the fastest MATLAB data processing!
- cell arrays
- structures, where you can include fields for each kind of data (e.g. Process type, Flow data, Temperature data, Notes, Units, etc). Structures can even be non-scalar, which is extremely useful! And yes, you can even define structure fieldnames dynamically.

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://www.mathworks.com/matlabcentral/newsreader/view_thread/240867

http://blogs.mathworks.com/loren/2005/12/28/evading-eval/

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

http://ww.mathworks.com/matlabcentral/answers/143-how-do-i-make-a-series-of-variables-a1-a2-a3-a10

http://www.mathworks.com/matlabcentral/answers/51946-systematic-do-not-use-global-don-t-use-eval

http://www.mathworks.com/matlabcentral/answers/74778-loop-file-and-variablenames-when-importin

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:

http://www.mathworks.com/matlabcentral/answers/225435-save-variable-as-string-from-user-input

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

Log in to comment.

Answer by Amit
on 22 Jan 2014

Edited by Amit
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
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
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

Log in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Muna (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/105936-how-to-make-dynamic-variable-names-a1-a2-an-with-for-loop-using-eval-num2str#comment_191573

Log in to comment.