exit from a nested while loop after having set its index to a given value

1 回表示 (過去 30 日間)
Riccardo Tronconi
Riccardo Tronconi 2021 年 6 月 8 日
編集済み: Cris LaPierre 2021 年 6 月 9 日
I would like to exit from a nested while loop with return but before I would like to set my index to a given value as it follows:
function [H12] = samehorizon(F1,F2)
%UNTITLED Summary of this function goes here
% Il counter non si azzera mai e non riesco a incrementare l'indice i
alltime= [F1{:,1};F2{:,1}];
t1= min(alltime);
t2= max (alltime);
thor= t1: seconds(1):t2;
thor=thor';
thor=array2table(thor);
A= zeros(height(thor), 7);
H12= array2table(A);
H12.A1 = NaT(height(thor),1,'Format', 'dd-MMM-yyyy HH:mm:ss.SSSSSSSSS' );
prova=1;
for i=1:height(thor)
j=prova;
counter2=0;
Xtot2=0;
Ytot2=0;
H12(i,1)= thor(i,1);
H12(i,5)= F2(1,2);
while j <=height(F2)
if j== height(F2)
break
end
if (thor{i,1} - F2{j,1}) > duration('00:00:02.000000000')
prova=j-3;
break
end
if thor{i,1} - F2{j,1} <= duration('00:00:01.000000000')
Xtot2= Xtot2 + F2{j,3};
Ytot2= Ytot2 + F2{j,4};
counter2= counter2+1;
Xmean2 = (Xtot2/counter2);
Ymean2= (Ytot2/counter2);
H12{i,6}= Xmean2;
H12{i,7}= Ymean2;
end
j=j+1;
end
end
end
The error is: Index in position 1 is invalid. Array indices must be positive integers or logical values. referred to this line. In fact, prova always is equal to one...

採用された回答

Cris LaPierre
Cris LaPierre 2021 年 6 月 8 日
We don't have enough data to run your example. However, the error message has to do with how you are indexing height. It would appear either thor or F2 are not a valid index.
a = 1:3;
% valid
a(1)
ans = 1
% invalid
a(1.5)
Array indices must be positive integers or logical values.
  6 件のコメント
Cris LaPierre
Cris LaPierre 2021 年 6 月 9 日
That is may be what you intended, but not what is happening. I included a try-catch to see when the error is occuring. Here are the results.
i = 5
j = -2
thor{i,1} = 05-Mar-2021 11:41:40.619605504
F2{j,1} =
Error using Untitled>samehorizon (line 114) % (this is my test script)
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Cris LaPierre
Cris LaPierre 2021 年 6 月 9 日
編集済み: Cris LaPierre 2021 年 6 月 9 日
Ok, here are some more details. Right before the error happens, here are the values:
i=3
j=79328
prova=1
counter2=79321
I've attached the full list.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by