Help needed with while loop code

1 回表示 (過去 30 日間)
D.J
D.J 2018 年 8 月 29 日
コメント済み: D.J 2018 年 8 月 29 日
Hi all. I am trying to use while-loop to determine when the cumulative rainfall for a particular month exceeds 8 mm. I have attached the file that I imported for my data. Also, this is the code that I used, but didn't work. Any help would be highly appreciated !
Testdata = importfile2('Test_data.csv', 2, 32);
%Parameters
RF=Testdata(1:31,4);
%Variables
D_var=0; % day
RF_tot = 0; % Rainfall total
%Condition
%RF_tot <=7
while RF_tot<=8
RF_tot=RF_tot+RF
D_var=D_var+1
end
  1 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 8 月 29 日
RF (Test Data) is not used anywhere in the code?

サインインしてコメントする。

採用された回答

Torsten
Torsten 2018 年 8 月 29 日
編集済み: Torsten 2018 年 8 月 29 日
while RF_tot<=8 && D_var <= 30
D_var=D_var+1
RF_tot=RF_tot+RF(D_var)
end
Best wishes
Torsten.
  3 件のコメント
Torsten
Torsten 2018 年 8 月 29 日
編集済み: Torsten 2018 年 8 月 29 日
As far as I understood your code, array element RF(D_var) contains the rainfall at day D_var of a certain month. If this is the case, these values RF(1),RF(2),...,RF(31) have to be added together to get the total rainfall of the month. The condition to stop is that the total rainfall up to day "D_var" exceeds 8 mm.
Thus you could equivalently do
RF_total = 0.0;
for D_var = 1:31
RF_total = RF_total + RF(D_var);
if RF_total > 8
break
end
end
D.J
D.J 2018 年 8 月 29 日
@Torsten Many thanks. This makes perfect sense. Much appreciated.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeArduino Hardware についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by