for loop break alternative
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Walter Roberson
le 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
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!