Clear Filters
Clear Filters

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

4 views (last 30 days)
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

Accepted Answer

Guillaume
Guillaume on 9 Mar 2016
Edited: Guillaume on 9 Mar 2016
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 Comments
pamela sulis
pamela sulis on 9 Mar 2016
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 on 9 Mar 2016
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!