Undefined variable error while using for loop.

2 views (last 30 days)
X_1=10
for i=0
X_(i+1)
Undefined function or variable 'X_i'. I wanna create X_1 with for loop, how can I create X_1 in the loop?

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 11 Jun 2013
Edited: Azzi Abdelmalek on 11 Jun 2013
i=1;
evalin('base',sprintf('X_%d=10',i))
  9 Comments
Azzi Abdelmalek
Azzi Abdelmalek on 19 Jun 2013
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
Jan
Jan on 19 Jun 2013
@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.

Sign in to comment.

More Answers (2)

Iain
Iain on 11 Jun 2013
for i = 1:whatever
eval(['X_' num2str(i) ' = value;'])
end
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;
end
Because that sets up a cell array (in X), and a numeric array (in Y), rather than a set of differently named variables.
  6 Comments
Iain
Iain on 19 Jun 2013
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.
Jan
Jan on 19 Jun 2013
@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.

Sign in to comment.


Jan
Jan on 18 Jun 2013
Summary: Don't do this. Create a (cell) array instead.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!