MATLAB Answers


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

Asked by Italo
on 13 Nov 2013
Latest activity Edited by per isakson
on 16 Feb 2017

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'])

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

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

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'])

Log in to comment.

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:

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:

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


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:


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 '

Log in to comment.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today