continue statment not executing correctly within for loop
조회 수: 1 (최근 30일)
이전 댓글 표시
I have the following code:
for i=1:length(files)
for j=1:length(files(i,1).day)
if ((1:length(files(i,1).day(j).hour)) ~= 24)
continue
end
for h = 1:24
if ((1:length(files(i,1).day(j).hour(h).halfhour)) ~= 2)
continue
end
if isempty(1:length(files(i,1).day(j).hour))
continue
end
dayPSD = [];
for h2=1:2
dayPSD = [dayPSD; files(i,1).day(j).hour(h).halfhour(h2).data{8}'];
end
end
end
end
where
files=1x3 structure
files(i,1).day=1x335 structure that contains 1 field hour which is a 1x24 structure
files(i,1).day.hour=1x24 structure that contains 1 field halfhour which is a 1x2 structure
I am trying to loop through i and j, but when 1:length(files(i,1).day(j).hour) ~= 24 (so when the hours are less than 24) I would like to exit the loop and continue onto the next iteration of the for loop. I would also like it to exit the loop and continue onto the next iteration of the for loop if 1:length(files(i,1).day(j).hour(h).halfhour) ~= 2 (so when the half hours are less than 2) or if 1:length(files(i,1).day(j).hour is an empty cell (no data).
The current code I have written only seems to skip a loop index if 1:length(files(i,1).day(j).hour ~=24.
What am I doing wrong? Also how do I get it to continue onto the next iteration if any of those conditions are met? I've been banging my head on this for a while, and I know I'm probably making a simple mistake, but if anyone can point me in the right direction that would be great.
Thanks for your help in advance!
Here's the updated answer after Jan's input and a little bit of tweaking:
for i=1:length(files)
for j=1:length(files(i,1).day)
if ((length(files(i,1).day(j).hour)) ~= 24)
continue
end
for h=1:24
if ((length(files(i,1).day(j).hour(h).halfhour)) ~= 2)
break
end
if isempty(1:length(files(i,1).day(j).hour))
break
end
dayPSD = [];
numDayPSDs = 0;
for h2=1:2
dayPSD = [dayPSD; files(i,1).day(j).hour(h).halfhour(h2).data{8}'];
numDayPSDs = numDayPSDs+1;
end
end
Thanks!
댓글 수: 0
채택된 답변
Jan
2014년 1월 5일
편집: Jan
2014년 1월 5일
What is the intention of this line:
if ((1:length(files(i,1).day(j).hour)) ~= 24)
Perhaps you mean:
if length(files(i,1).day(j).hour) ~= 24
If files(i,1).day(j).hour is a vector, 1:files(i,1).day(j).hour is the vector from 1 to the first element of files(i,1).day(j).hour .
You can use the debugger to investigate such problems. Set a breakpoint in this line and check the values in the command line:
(1:length(files(i,1).day(j).hour)) ~= 24
1:length(files(i,1).day(j).hour)
length(files(i,1).day(j).hour)
files(i,1).day(j).hour
It is a bug (beside the same "1:x" problem) to check
if isempty(1:length(files(i,1).day(j).hour))
after
if ((1:length(files(i,1).day(j).hour(h).halfhour)) ~= 2)
If hour is empty, you cannot access files(i,1).day(j).hour(h).halfhour .
추가 답변 (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!