How to find string structure elements?

14 vues (au cours des 30 derniers jours)
Csaba
Csaba le 2 Mai 2017
Réponse apportée : upol le 10 Jan 2019
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

Réponse acceptée

Azzi Abdelmalek
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
Stephen23 le 3 Mai 2017
Csaba
Csaba le 3 Mai 2017
Thank you!

Connectez-vous pour commenter.

Plus de réponses (1)

upol
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

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!

Translated by