Looping through Structure Elements

조회 수: 2 (최근 30일)
Franco . 2013년 2월 28일
I have this code, where I am trying to loop through structure elements.
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);
Anyone know why this is telling me that Electrical.Twelve.February doesnt exist?

채택된 답변

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:
This will build an empty structure.
  댓글 수: 2
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 CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by