Looping through Structure Elements

1 回表示 (過去 30 日間)
Franco
Franco 2013 年 2 月 28 日
I have this code, where I am trying to loop through structure elements.
month2=struct('January',1,'February',2,'March',3,'April',4,'May',5,'June',6,'July',7,'August',8,'September',9,'October',10,'November',11,'December',12);
year2 = struct('Twelve',1,'Thirteen',2);
fieldmonths = fieldnames(month2);
fieldyear = fieldnames(year2);
for i = 1:numel(fieldyear)
for j = 1:numel(fieldmonths)
temp1 = Electrical.(fieldyear{i}).(fieldmonths{j})(1,1);
end
end
Anyone know why this is telling me that Electrical.Twelve.February doesnt exist?

採用された回答

Leah
Leah 2013 年 2 月 28 日
Because you are referencing Electrical.Twelve.February before it's created.
I'm not sure what your code is trying to do, but try replacing the temp1 line inside the loop with this:
Electrical.(fieldyear{i}).(fieldmonths{j})=[];
This will build an empty structure.
  2 件のコメント
Franco
Franco 2013 年 2 月 28 日
I guess I should have added that Im crating the structure before..Ooops!
for i = 1:a3
if date_4(i,1) == 1 && date_4(i,3) == 12;
Electrical.Twelve.January(i,:) = electrical_data(i,:);
elseif date_4(i,1) == 2 && date_4(i,3) == 12;
Electrical.Twelve.February(i,:) = electrical_data(i,:);
else
continue
end
end
Jan
Jan 2013 年 2 月 28 日
@Franco: But the error message tells you unequivocally, that Electrical.Twelve.February does not exist. Perhaps the is no "date_4(i,3) == 12". While you can check this using the debugger, we can only guess these details.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by