Specifying elements of a structure array that have different fields
Afficher commentaires plus anciens
Hello -
I'm using a structure array to describe a set of particles that can have very different properties. I've found that by creating a structure field for each element of the array, I can then add specific fields that may not be shared by all members of the array:
particle(1).type = 'human';
particle(1).properties.name = 'Barney';
particle(1).properties.gender = 'm';
particle(1).properties.weight = 190.0;
particle(2).type = 'vehicle';
particle(2).properties.model = 'Camry';
particle(2).properties.weight = 3000.0;
particle(2).properties.length = 14.0;
Is this permissible to do? Thanks.
Réponses (1)
To get a logical index into all 'human':
humanIdx = cellfun(@(x) strcmp('human',x),{particle(:).type})
Please accept an answer if it helped you.
Catégories
En savoir plus sur Structures dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!