Effacer les filtres
Effacer les filtres

Modify values in a struct array

22 vues (au cours des 30 derniers jours)
DC
DC le 1 Avr 2020
Commenté : DC le 1 Avr 2020
I would like to modify the values of a field of a struct array (without using a for loop). If my initial struct array is the following:
a.b(1).c = 5;
a.b(2).c = 5;
a.b(3).c = 5;
I would like to modify it such that:
a.b(1).c = 1
a.b(2).c = 2
a.b(3).c = 3
I thought that
[a.b.c] = [1:3]
would work, but it doesn't: Insufficient number of outputs from right hand side of equal sign to satisfy assignment.

Réponse acceptée

Walter Roberson
Walter Roberson le 1 Avr 2020
newvalues = 1:3;
temp = num2cell(newvalues);
[a.b.c] = temp{:};
Unfortunately this is not easy to do on one line, at least not without using a helper function.
Expand = @(temp) temp{:};
[a.b.c] = Expand(num2cell(1:3));
  1 commentaire
DC
DC le 1 Avr 2020
Thank you, that works!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by