필터 지우기
필터 지우기

for loop with an exclusion

조회 수: 9 (최근 30일)
Mich
Mich 2014년 11월 1일
댓글: Mich 2014년 11월 1일
I'd like to run a particular code through a loop where n_datafiles = [1,2,3,4,6,7,8] while skipping the number 5.
n_datafiles = [1, 2, 3, 4, 6, 7, 8]; for i_datafile = 1:n_datafiles %my code end
I've tried the above code, but it doesnt work. I get the error saying that the index exceeds matrix dimensions.
I've also tried to use n_datafile == 1:4 && n_datafiles = 6:8 but i get an error saying that there is an unexpected MATLAB operator.
For now, the only way I can get my code to work is to separate the code into 2 for loops- 1:4 and 6:9. I read that I should avoid using a for loop because it will mess with the indexing. However, I need to use a for loop in this case as it involves reading and saving the file in [name]_(1/2/3/4/6/7/8) respectively.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 11월 1일
편집: Azzi Abdelmalek 2014년 11월 1일
Why 1:n_datafiles ? it's just n_datafiles
n_datafiles = [1, 2, 3, 4, 6, 7, 8];
for i_datafile =n_datafiles
%my code
end
  댓글 수: 1
Mich
Mich 2014년 11월 1일
oh thanks! I might have overlooked that part as my previous code was n_datafiles = 1:8.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by