How to update a field value in a Struct Array

22 visualizaciones (últimos 30 días)
Mohammad Tabesh
Mohammad Tabesh el 8 de Abr. de 2011
Comentada: Guillaume el 16 de Mzo. de 2015
I am trying to update a field value in a struct array. For example if I have 1*10 struct of A with a field in it called B, I want to replace the following loop with another method:
for iLoop=1:10
A(iLoop).B = 1;
end
The best thing I found is to use the setfield() function as:
A = setfield(A,num2cell(1:10),'B',num2cell(1),1);
This results in a 10-D struct which I cannot understand why. Does anyone know a way of doing this?

Respuesta aceptada

Walter Roberson
Walter Roberson el 8 de Abr. de 2011
[A(1:10).B] = 1;
  4 comentarios
Alex
Alex el 16 de Mzo. de 2015
What if
A(1).B=[ 0 0 0 0 0], A(2).B=[0 0 0]...A(10).B=[0 0 0 0]
and
list=[ 4, 2,...,3 ]
The how would you change the indices specified in list such that
A(1).B=[0 0 0 1 0], A(2).B=[0 1 0]...A(10).B=[0 0 1 0]
?
Guillaume
Guillaume el 16 de Mzo. de 2015
Rather than hijacking a very old question, start your own question. Gives somebody a chance to get the credits for the answer.

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