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

1 view (last 30 days)
Riccardo Tronconi on 8 Jun 2021
Edited: Cris LaPierre on 9 Jun 2021
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 on 8 Jun 2021
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.
Cris LaPierre on 9 Jun 2021
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.