Stop for loop if value is bigger than 10

조회 수: 6 (최근 30일)
Dion Theunissen
Dion Theunissen 2021년 3월 26일
댓글: Chien-Han Su 2021년 3월 26일
Hi,
I want to add a while loop and start the for loop again if a value in dx > 10. So I have to stop the rest of the loop and startover for the next for loop.
Check my script where i want to add the while loop.
for i1 = 1:length(RCA);
fullfilename = fullfile(RCA(i1).folder, RCA(i1).name);
thisData = readmatrix(fullfilename, 'Range', 2, 'Delimiter', ',');
sample_dist = thisData(:,1);
step = (max(sample_dist)-min(sample_dist))/2499;
a = min(sample_dist):step:max(sample_dist);
XYZ = [thisData(:,7),thisData(:,8),thisData(:,9)];
vq = interp1(sample_dist, XYZ, a);
dx = vq(:,1) - vq(1,1);
dy = vq(:,2) - vq(1,2);
dz = vq(:,3) - vq(1,3);
vq(1,1) = 0;
vq(1,2) = 0;
vq(1,3) = 0;
% here i want to check if any value in dx is bigger than 10. if yes
% start the for loop again for the next set. If no continue this loop
plot3(dx,dy,dz)
legend(strcat(i1))
hold on
afstand=[1:2500, i1];
lengte = size(dx);
lengte = round(lengte);
row = 0;
for i2 = 1:1:lengte-1
row = row+1;
P1 = [dx(i2), dy(i2), dz(i2)];
P2 = [dx(i2+1), dy(i2+1), dz(i2+1)];
P(i2,1:3) = [dx(i2), dy(i2), dz(i2)];
angleradian2(i2) = acos((P2(2)-P1(2)) / norm(P2-P1));
distance(i2) = norm(P1 - P2);
end
avgrad = mean(angleradian2,'all','omitnan')
for i2 = 1:250:lengte-250
row = row+1;
P1 = [dx(i2), dy(i2), dz(i2)];
P2 = [dx(i2+250), dy(i2+250), dz(i2+250)];
P(i2,1:3) = [dx(i2), dy(i2), dz(i2)];
% angleradian2(i2) = acos((P2(2)-P1(2)) / norm(P2-P1));
angleradian3(row,i1) = acos((P2(2)-P1(2)) / norm(P2-P1));
distance(i2) = norm(P1 - P2);
end
angleradian2 = angleradian2(:,1);
afstand = afstand(:,1:2500);
end

채택된 답변

Chien-Han Su
Chien-Han Su 2021년 3월 26일
I think what you need is the 'continue' command, try
...
% here i want to check if any value in dx is bigger than 10. if yes
% start the for loop again for the next set. If no continue this loop
if dx > 10
continue
end
plot3(dx,dy,dz)
...
or you can check https://www.mathworks.com/help/matlab/ref/continue.html for more information
  댓글 수: 2
Dion Theunissen
Dion Theunissen 2021년 3월 26일
dx is not a value, its a list. So I have to check if any value in dx is > 10. How can i see that?
Chien-Han Su
Chien-Han Su 2021년 3월 26일
Then just fix it as follows
...
% here i want to check if any value in dx is bigger than 10. if yes
% start the for loop again for the next set. If no continue this loop
if any(dx > 10)
continue
end
plot3(dx,dy,dz)
...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by