How to find string structure elements?

14 views (last 30 days)
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

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 2 May 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)

More Answers (1)

upol
upol on 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

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!