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
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 个)
另请参阅
类别
在 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!