continue statment not executing correctly within for loop

1 回表示 (過去 30 日間)
Katherine
Katherine 2014 年 1 月 5 日
編集済み: Katherine 2014 年 1 月 5 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by