【发布时间】:2014-01-05 21:31:31
【问题描述】:
我有以下代码:
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
在哪里
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 和 j,但是当 1:length(files(i,1).day(j).hour) ~= 24 时(所以当小时小于 24 时)我想退出循环并继续进行 for 循环的下一次迭代。如果 1:length(files(i,1).day(j).hour(h).halfhour) ~= 2 (所以当半小时小于 2) 或者如果 1:length(files(i,1).day(j).hour 是一个空单元格(无数据)。
我编写的当前代码似乎仅在 1:length(files(i,1).day(j).hour ~=24 时跳过循环索引。
我做错了什么?另外,如果满足任何这些条件,我如何让它继续进行下一次迭代?我一直在努力解决这个问题,我知道我可能犯了一个简单的错误,但如果有人能指出我正确的方向,那就太好了。
提前感谢您的帮助!
【问题讨论】:
-
您能否更具体地说明在提及时要跳过哪个循环索引?你有两个嵌套循环;
if ((1:length(files(i,1).day(j).hour)) ~= 24)中的continue将跳过for j=1:length(files(i,1).day)循环的一次迭代,而if ((1:length(files(i,1).day(j).hour(h).halfhour)) ~= 2)中的continue和if sempty(1:length(files(i,1).day(j).hour))中的一个将跳过for h = 1:24循环的迭代。 -
我希望他们两个都能跳出 j 循环索引
-
然后尝试用
breaks 替换continues(见我的回答)