필터 지우기
필터 지우기

Is it possible to use 'numel' in nested struct?

조회 수: 4 (최근 30일)
pamela sulis
pamela sulis 2016년 3월 9일
댓글: Guillaume 2016년 3월 9일
Hi! I use numel in a nested struct:
for i=1:9
for j=1:size(E(1,i).bcd,2)
for jj=1:numel(E(1,i).bcd{1,j}.b)
if(isempty(E(1,i).bcd{1,j}.b{jj}))
E(1,i).bcd{1,j}.b{jj}=''
end
end
end
end
but it gives this error
'Attempt to reference field of non-structure array.'
Is it possible that the error is in the use of numel? thanks

채택된 답변

Guillaume
Guillaume 2016년 3월 9일
편집: Guillaume 2016년 3월 9일
No, the error has nothing to do with numel. As the error says, you're attempting to access a field (b maybe) of something that is not a structure.
Assuming that E is really a structure. One of the cell array in the bcd fields does not contain a structure.
To check
for eidx = 1:numel(E)
for bcdidx = 1:numel(E(eidx).bcd)
if ~isstruct(E(eidx).bcd{bcdidx})
warning('E(%d).bcd{%d} is not a structure', eidx, bcdidx);
end
end
end
  댓글 수: 2
pamela sulis
pamela sulis 2016년 3월 9일
I try your code and I have an other error: 'Scalar index required for this type of multi-level indexing'. Thanks for your suggestion: surely there is an error in the dimensions of the struct or in the definition of the struct.
Guillaume
Guillaume 2016년 3월 9일
I did make a typo in my code (used idx instead of eidx, most likely you already had a idx variable in your workspac). I've fixed it now and the code should run without error.
The problem is most likely that one of your cell array bcd is empty.

댓글을 달려면 로그인하십시오.

추가 답변 (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