Effacer les filtres
Effacer les filtres

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

3 vues (au cours des 30 derniers jours)
pamela sulis
pamela sulis le 9 Mar 2016
Commenté : Guillaume le 9 Mar 2016
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

Réponse acceptée

Guillaume
Guillaume le 9 Mar 2016
Modifié(e) : Guillaume le 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 commentaires
pamela sulis
pamela sulis le 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 le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by