Undefined variable error while using for loop.

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일

0 개 추천

i=1;
evalin('base',sprintf('X_%d=10',i))

댓글 수: 9

sermet
sermet 2013년 6월 11일
you don't get it exactly I suppose, your answer doesn't include any loop. It just create X_0=10. What I mean is I have to create X_1,X_2,X_3 and 10 and other numbers have to assign into X_1,X_2,X_3 by for loop.
for i=1:10
data=10;
evalin('base',[sprintf('X_%d=',i) num2str(data)])
end
sermet
sermet 2013년 6월 11일
your answer creates string data, what I want is opening loop without data and when X_1 is created this value would automatically assigned because X_1 is predetermined before.
No, it's not string data. What do you mean by X_1 is created?
sermet
sermet 2013년 6월 11일
X_1, X_2,X_3,X_4 how can I create these variates by increasing 1 with for loop
Semet, you can not create a variable without assigning it a value.
for i=1:10
evalin('base',sprintf('X_%d=[]',i))
end
a variable exist if it contains a value, unless you are using symbolic functions
Jan
Jan 2013년 6월 18일
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
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일

2 개 추천

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

Jan
Jan 2013년 6월 18일
@lain: I really do agree with your suggestion not to hide an index in the name of the variable. Why inventing a complicated method to do this only to be forced to use equivalently difficult methods later, when the variables should be accessed? +1
Tom
Tom 2013년 6월 18일
편집: Tom 2013년 6월 18일
I think it's worth noting that of the MATLAB I was taught at university, cell arrays/ structures never came up- I remember finding out the EVAL solution to this problem before I even knew what a cell array was, and it seems like I'm not alone in this.
Jan
Jan 2013년 6월 18일
Cells have been introduced in Matlab 5.0/1997 (as far as I remember). Perhaps your teacher had learned Matlab with an earlier version. But teaching Matlab without mentioning such fundamental built-in data types is an outrage. At least reading the Getting Started chapters of the documentation do not conceal this and reading the FAQ is a good idea also. when such a powerful language as Matlab should be applied efficiently.
Tom, let me ask if you could be so kind to send your teacher to this forum, such that he can brush up his knowledge and does not forget to tell all important details.
Tom
Tom 2013년 6월 19일
@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.
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일

0 개 추천

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

카테고리

도움말 센터File 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