Stop for loop if value is bigger than 10

3 次查看(过去 30 天)
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 中查找有关 MATLAB Compiler 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by