MATLAB Answers

Stop for loop if value is bigger than 10

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

Accepted Answer

Chien-Han Su
Chien-Han Su on 26 Mar 2021
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 Comments
Chien-Han Su
Chien-Han Su on 26 Mar 2021
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)
...

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by