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?
채택된 답변
추가 답변 (2개)
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
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
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
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
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
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
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!