Simple if loop doesn't work

1 view (last 30 days)
Justin Moreault
Justin Moreault on 30 May 2018
Commented: Justin Moreault on 31 May 2018
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.

Answers (1)

John D'Errico
John D'Errico on 30 May 2018
Edited: John D'Errico on 30 May 2018
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 Comment
Justin Moreault
Justin Moreault on 31 May 2018
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!

Sign in to comment.

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!