for loop break alternative
조회 수: 5 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
Walter Roberson
2022년 10월 12일
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!