how to restart a for loop from first iteration if a condition work.. in this code i want to return to first iteration of for loop after the if condition true and display the error
25 ビュー (過去 30 日間)
古いコメントを表示
fs=input('Enter the sampling Freq=');
tstart=input('Enter start time=');
tend=input('Enter end time=');
NB=input('Enter number of breakpoints=');
t(1)=tstart; t(NB+2)=tend;
for i=2:NB+1
t(i)=input(sprintf('Enter the position of break point %d=',i-1));
if (t(i)<tstart || t(i)>tend)
disp('Erorr!!.. Enter the position of break points within the range of time');
end
end
0 件のコメント
回答 (1 件)
KALYAN ACHARJYA
2018 年 11 月 20 日
編集済み: KALYAN ACHARJYA
2018 年 11 月 20 日
for i=1:iterations_number
%do your finding
if condition true
i=1; %assign i value again 1 (initial), first iteration
end
end
Your case
fs=input('Enter the sampling Freq=');
tstart=input('Enter start time=');
tend=input('Enter end time=');
NB=input('Enter number of breakpoints=');
t(1)=tstart; t(NB+2)=tend;
for i=2:NB+1
t(i)=input(sprintf('Enter the position of break point %d=',i-1));
if (t(i)<tstart || t(i)>tend)
disp('Erorr!!.. Enter the position of break points within the range of time');
i=2;
end
end
10 件のコメント
Image Analyst
2018 年 11 月 21 日
編集済み: Image Analyst
2018 年 11 月 21 日
Note that if you change the loop iterator value inside the loop, it takes effect only until the bottom of the loop. When the loop starts the next iteration, it will have the same value it would have had, had you not changed it. It will not start at the next number after the number you changed it to. So if you do
for k = 1 : 100
k = 30;
end
so that on the very first iteration k gets set to 30, the second iteration will start with k=2, not with k=31.
It's not recommended to ever change the loop variable because of this high possibility of being confusing/deceptive/unexpected.
参考
カテゴリ
Help Center および File Exchange で Filter Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!