MATLAB Answers


Undefined variable error while using for loop.

Asked by sermet on 11 Jun 2013


for i=0


Undefined function or variable 'X_i'. I wanna create X_1 with for loop, how can I create X_1 in the loop?


Log in to comment.


3 Answers

Answer by Azzi Abdelmalek
on 11 Jun 2013
Edited by Azzi Abdelmalek
on 11 Jun 2013
 Accepted Answer



I definitely recommend not to use sich EVAL/EVALIN approachs, which increase the complexity of the program and reduce the speed massively.

@Azzi: I remember that you did not agree with the rule to avoid eval (see, Answers: don't use eval). But the answers you got ans the FAQ contain very good arguments for avoiding EVAL. Of course I do not try to and cannot force you to be convinced. After more than 10 years in Matlab forums I learned, that asnwers suggesting complicated EVAL methods to hide an index in created variables provoke further questions about complicated methods to access these variables later on.

Therefore I recommend another time to believe the FAQ, that avoiding EVAL makes the programming easier.

Jan, I've said: did not agree with systematic avoid EVAL, I have no doubt about the problems you are mentioning. I think, everyone has the right to know how to use these function (EVAL,...), even it's not recommended for the reasons given in the FAQ, maybe it could help him to finish his work, then after he will reconsider the way of programming. Myself, and after being in Matlab forum for almost one year, and reading different answers and comment, I've reconsidered many things about Matlab programming like avoiding EVAL when it's possible

@Azzi: Ok. Of course a Matlab programmer must know the EVAL command and should know how and why to avoid it. Therefore I will add the comment "Don't do this, read the FAQ instead", when you suggest EVAL/IN. Unfortunately this does not sound polite, but I mean the evil EVAL with this opposition.

The Matlab beginners, who run into the same old EVAL problem, owe our best advices. While EVAL will solve their problems for the next 5 minutes, applying more direct and clear programming methods will help for the rest of their programming life.

We do not have to agree in this point. Different opinions and oppositions are valuable. I think the answers to your question are unequivocal and I'm convinced, that suggesting EVAL/IN let the asking persons suffer more than necessary.

Log in to comment.

Answer by Iain
on 11 Jun 2013

 for i = 1:whatever
  eval(['X_' num2str(i) ' = value;'])

What that does is set X_1, X_2, X_3, X_4... X_whatever equal to whatever value is.

I would suggest, instead, that you use

 for i = 1:whatever
  X{i} = value;
  Y(i) = value;

Because that sets up a cell array (in X), and a numeric array (in Y), rather than a set of differently named variables.


@Jan, I think the issue is that we weren't taught it as you would teach computer science students, we were taught how to do the things we needed to do for coursework etc., and then left to our own devices. This was a few years ago and I hear they're overhauling it; I hope so as the use of programming in engineering is only going to increase.

The majority of engineers aren't taught good programming practices in any computing language. I was taught about a wide range of electronic and aerospace systems rather than "programming".

I was taught to avoid "goto", but I've never had a problem with it.

I was never taught anything about function handles or "eval", evalin, or assignin, I discovered them on my own, and I've come to realise, through use, that both are very slow, and should be avoided.

@Tom & lain: Thanks for your answer. I know, that good programming patterns are a kind of concealed from engineers. But I see Matlab as a tool, which creates brilliant results only when it is handles excellently. Even computer scientists are not taught in computer lanuages very well and this is a big problem when after 100 perfectly solved homework questions with 100 to 1000 lines of code, the students have to write code for a PhD, which requires 100'000 lines of code. I've seen many people who tried to create 100 smaller scripts, which communicate over global variables and provide results by EVALIN them in another workspace.

Therefore I decided to offer some solutions in this forum. And perhaps it helps the one or other programmer.

Log in to comment.

Answer by Jan Simon
on 18 Jun 2013

See also: FAQ: How can I create the variables A1, A2,... in a loop.

Summary: Don't do this. Create a (cell) array instead.


Log in to comment.

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!