Effacer les filtres
Effacer les filtres

Access the fields in a structure array specified in a list of strings.

2 vues (au cours des 30 derniers jours)
Colin Phipps
Colin Phipps le 16 Juin 2016
Commenté : Colin Phipps le 17 Juin 2016
I have a lot of numbers stored in a massive structure, and I need to access specific fields. So a minimal example, let say I have:
X.puppy=7;
X.kitten=4;
X.duckling=2;
and I want to access these fields:
furry={'puppy','kitten'};
Is there a way to do this without a loop?
I don't care what the format of the output is since I can easily convert, I just need something that outputs: [7 4].
Thanks for any help in finding my X.(furry).

Réponse acceptée

Image Analyst
Image Analyst le 16 Juin 2016
Try this:
X.puppy=7;
X.kitten=4;
X.duckling=2;
furry={'puppy','kitten'};
% Convert to table
t = struct2table(X)
% Extract numbers.
out = t{1, furry(:)}
  1 commentaire
Colin Phipps
Colin Phipps le 17 Juin 2016
Thank you. The day has come where I appreciate tables! It's possible that they're a more natural option for my data storage in the first place, I'll have to think about it.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by