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

5 visualizaciones (últimos 30 días)
pamela sulis
pamela sulis el 9 de Mzo. de 2016
Comentada: Guillaume el 9 de Mzo. de 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

Respuesta aceptada

Guillaume
Guillaume el 9 de Mzo. de 2016
Editada: Guillaume el 9 de Mzo. de 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 comentarios
pamela sulis
pamela sulis el 9 de Mzo. de 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 el 9 de Mzo. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by