for loop break alternative

5 views (last 30 days)
Aya
Aya on 12 Oct 2022
Answered: Walter Roberson on 12 Oct 2022
i have 3 for loops in my code, in the first for loop, a break is included in my if statment. apparently it breaks the whole code and does not move on to the for loop.
how can i fix this?
my code:
%DATA CLEANING
%initialising
s = 3; %3 cuz in 4loop 3-3 will give error
%CLEANING ARARAT WIND DATA
new_times_ararat = cell(52560,1);
for y=1:size(ararat_org)
s=s+1; %put limit to stop before
if s<52557
up3 = sum(new_data_info(s-3:s-1,10));
down3 = sum(new_data_info(s+1:s+3,10));
v_avg = (up3+down3)/6;
elseif s>52557
break
end
if new_data_info(s,10)<0
new_data_info(s,10) = v_avg;
%storing time of edit
t= datetime('now','Format','ydMHm');
new_times_ararat{s,1} = t; %this is so time is in the same row as the edited data.
elseif new_data_info(s,10)/v_avg > 1.7 %wb the 0.3?
new_data_info(s,10) = v_avg;
%storing time of edit
t= datetime('now','Format','ydMHm');
new_times_ararat{s,1} = t;
end
end
%CLEANING SILVERTOONE DATA
new_times_silvertoon = cell(52560,1);
for y=1:size(silvertoon_org)
s=s+1; %put limit to stop before
if s<52557
up3 = sum(new_data_info(s-3:s-1,11));
down3 = sum(new_data_info(s+1:s+3,11));
v_avg = (up3+down3)/6;
elseif s>52557
break
end
if new_data_info(s,11)<0
new_data_info(s,11) = v_avg;
%storing time of edit
t= datetime('now','Format','ydMHm');
new_times_ararat{s,1} = t; %this is so time is in the same row as the edited data.
elseif new_data_info(s,11)/v_avg > 1.7 %wb the 0.3?
new_data_info(s,11) = v_avg;
%storing time of edit
t= datetime('now','Format','ydMHm');
new_times_ararat{s,1} = t;
end
end
%CLEANING BOCOROCK DATA
new_times_bocorock = cell(52560,1);
for y=1:size(bocorock_org)
s=s+1; %put limit to stop before
if s<52557
up3 = sum(new_data_info(s-3:s-1,12));
down3 = sum(new_data_info(s+1:s+3,12));
v_avg = (up3+down3)/6;
elseif s>52557
break
end
if new_data_info(s,12)<0
new_data_info(s,12) = v_avg;
%storing time of edit
t= datetime('now','Format','ydMHm');
new_times_ararat{s,1} = t; %this is so time is in the same row as the edited data.
elseif new_data_info(s,12)/v_avg > 1.7 %wb the 0.3?
new_data_info(s,12) = v_avg;
%storing time of edit
t= datetime('now','Format','ydMHm');
new_times_ararat{s,1} = t;
end
end

Answers (1)

Walter Roberson
Walter Roberson on 12 Oct 2022
break always terminates the for or while that it is inside. If you do not want to terminate the loop do not use break. Sometimes continue instead of break is appropriate

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!