Simple if loop doesn't work

My loop doesn't continue after first iteration. The two vectors Q and T do not have values when m=2. The file where I get my values is a table. H is the lenght of an excel file (table as well) with a number of different hydrometric stations from which I want the info
for m=1:H
Station=Sta{m,1};
Donnees=Donnees(strcmp(Donnees.NoStation, Station),:);
Q=Donnees{:,5};
T=Donnees{:,7};
[beta_qt,R_qt,J_qt,CovB_qt,MSE_qt]=nlinfit(Q,T,@(b,Q)(b(1)*Q.^(b(2))),[1 1]);
Tm=beta_qt(1)*Q.^beta_qt(2);
RMSE_qt=sqrt(MSE_qt);
A(m,1:3)=[Station beta_qt(1) beta_qt(2)];
end
I tried a while loop and it didn't help.

回答(1 个)

John D'Errico
John D'Errico 2018-5-30
编辑:John D'Errico 2018-5-30
What if loop? An if is not a loop. And you have no ifs in this code anyway. I'll assume you meant to say a for loop. A while loop would make no difference, and I have a funny feeling you don't understand why you might use a while loop anyway. A for loop is appropriate when you have a fixed set to iterate over, here that is 1:H. You would use a while loop if you don't know how long the loop will continue, but you know when it is done. (So a while loop is something SOME Supreme court justices would comprehend.) Regardless ...
What is Donnees? You use it almost like a function, here:
Donnees=Donnees(strcmp(Donnees.NoStation, Station),:);
at least on the left hand side. But then you overwrite it as a variable named Donnees. That is REALLY bad programming style.
Worse, then you seem to be accessing it as a cell array. Sheesh. Make up your mind. You are asking for bugs galore if you continue to write code like this.
Anyway, the second time through the loop, this will fail, because you just overwrote Donnees, whatever it is, on the first pass through the loop.

1 个评论

Yeah, I found out that what I was doing with Donnees wasn't good. Thanks for the critism, I'm just beginning to work with Matlab and programming in general!

请先登录,再进行评论。

类别

在 帮助中心 和 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