How to assign values to a struct array with fields?

111 visualizaciones (últimos 30 días)
SeaMoon Kim
SeaMoon Kim el 13 de En. de 2017
Comentada: madhan ravi el 4 de Jun. de 2019
Is there any way to write down a one-line script for assigning values to a struct array with fields?
x(1).a=1; x(2).a=2;
I'd like to change each value to 10 and 20 respectively. (I mean x(1).a=10, x(2).a=20) [x.a]=[10 20]; <- This causes an error.

Respuesta aceptada

Vandana Rajan
Vandana Rajan el 16 de En. de 2017
Editada: madhan ravi el 4 de Jun. de 2019
Hi,
>> x = struct('a',{10,20});
This will create x(1).a = 10 and x(2).a = 20
To modify an already existing structure array,
>> new_val = num2cell([10,20]);
>> [x.a] = new_val{:};
Please see the following link
  2 comentarios
SeaMoon Kim
SeaMoon Kim el 16 de En. de 2017
Thanks for your comment, Vandana. It works perfectrly! But I found that there is no way to assign struct array in one line. :-(

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