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

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by