MATLAB Answers

Italo
1

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

Asked by Italo
on 13 Nov 2013
Latest activity Answered by Stephen Cobeldick
on 12 Jan 2016

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

  1 Comment

Muna
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.

2 Answers

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:

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/56124-eval-is-evil-using-variables-created-dynamically-info-retrieval

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

http://www.mathworks.com/matlabcentral/answers/79281-create-a-structure-name-based-on-the-string-of-a-variable-create-fieldnames-in-this-structure

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):

http://www.mathworks.com/matlabcentral/answers/249044-code-fails-to-run-in-function-works-in-command-window-works-in-debug-evaluate-selection

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://www.reddit.com/r/learnpython/comments/2a9o4s/why_is_it_a_bad_idea_to_create_dynamic_variables/

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

  0 Comments


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

  2 Comments

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

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


Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!