continue statment not executing correctly within for loop

3 次查看(过去 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!

采纳的回答

Jan
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 .
  1 个评论
Katherine
Katherine 2014-1-5
You are right Jan, I was actually never meeting at least my second condition because I was comparing a vector with a scalar resulting in a vector of a boolean. I ended up changing both conditions and adding in some breaks out of the h loop to fix this. Thanks!

请先登录,再进行评论。

更多回答(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