Converting Struct field to array

1 386 vues (au cours des 30 derniers jours)
Gabriel Stanley
Gabriel Stanley le 9 Juin 2021
Modifié(e) : Stephen23 le 17 Juin 2023
I have an n-value structure array with multiple fields, and want to extract a specific field (all values thereof are (1x4) single arrays) into a nx4 array.
MyMatrix = [MyStruct(:).FieldC];
concatenates the values of the field, giving me a 1x(n*4) array. There's probably a way to reshape that, but I would appreicate a more direct solution as I'm doing this in the command window on an as-needed basis, rather than via a script.

Réponse acceptée

James Tursa
James Tursa le 9 Juin 2021
Does this do what you want?
MyMatrix = vertcat(MyStruct.Field);
  3 commentaires
Babak
Babak le 16 Juin 2023
This does not work if the field is 2 or more structure layers down from the indexing. e.g. d.field1.field2 where d is a structure array, e.g. d(1).field1.field2
Stephen23
Stephen23 le 17 Juin 2023
Modifié(e) : Stephen23 le 17 Juin 2023

Connectez-vous pour commenter.

Plus de réponses (1)

David Hill
David Hill le 9 Juin 2021
MyMatrix=cell2mat(struct2cell(MyStruct));
  2 commentaires
Gabriel Stanley
Gabriel Stanley le 9 Juin 2021
Editing my original post to the following effect: there are multiple field in the structure array, and I'm only looking to extract 1 of them to an array.
Matt Weber
Matt Weber le 11 Oct 2022
David Hill's solution worked for me, the other one did not. My context was a struct with just one field level.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by