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

1 view (last 30 days)
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...

Accepted Answer

Cris LaPierre
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.
  6 Comments
Cris LaPierre
Cris LaPierre on 9 Jun 2021
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 on 9 Jun 2021
Edited: 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.

Sign in to comment.

More Answers (0)

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!