How to find string structure elements?
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
May be it is obvious, but I could not find out.
Let's say I have a structure 'people' with fields 'Surname' and 'Family_Name', like this:
people(1).Surname='Judit';
people(1).Family_Name='White';
people(2).Surname='Margaret';
people(2).Family_Name='Brown';
people(3).Surname='Judit';
people(3).Family_Name='Brown';
and so on, thousends.
How to find all Judits (or Browns)? I know I can make loops but is there any faster and shorter code?
Csaba
0 commentaires
Réponse acceptée
Azzi Abdelmalek
le 2 Mai 2017
people(1).Surname='Judit';
people(1).Family_Name='White';
people(2).Surname='Margaret';
people(2).Family_Name='Brown';
people(3).Surname='Judit';
people(3).Family_Name='Brown';
%--------------------------------
a={people.Surname}
b={people.Family_Name}
id={'Judit' 'Brown'}
idx1=ismember(a,id)
idx2=ismember(b,id)
2 commentaires
Stephen23
le 3 Mai 2017
The syntax for converting the field to a cell array is documented here:
Plus de réponses (1)
upol
le 10 Jan 2019
Why this gives problem in C Coder. Error: Directly accessing field or property of nonscalar struct or object not supported for code generation.
s1=string({OPS_FLT(:).ACFT_ID})
s2=OPS_FLT(2).ACFT_ID
uuindex=find(strcmpi(s1,s2))
({OPS_FLT(:).ACFT_ID}) has already been defined as
OPS_FLT(1).ACFT_ID="apple"
OPS_FLT(2).ACFT_ID="orange"
I am trying to find orange from the array. It works in Matlab but not in C Coder
0 commentaires
Voir également
Catégories
En savoir plus sur Structures dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!