필터 지우기
필터 지우기

Undefined variable error while using for loop.

조회 수: 2 (최근 30일)
sermet
sermet 2013년 6월 11일
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?

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 6월 11일
편집: Azzi Abdelmalek 2013년 6월 11일
i=1;
evalin('base',sprintf('X_%d=10',i))
  댓글 수: 9
Azzi Abdelmalek
Azzi Abdelmalek 2013년 6월 19일
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 2013년 6월 19일
@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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Iain
Iain 2013년 6월 11일
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
Iain
Iain 2013년 6월 19일
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 2013년 6월 19일
@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.

댓글을 달려면 로그인하십시오.


Jan
Jan 2013년 6월 18일
Summary: Don't do this. Create a (cell) array instead.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by