can i access field by using values in structure?

2 vues (au cours des 30 derniers jours)
Urvashi
Urvashi le 10 Oct 2022
Commenté : Urvashi le 12 Oct 2022
if there is some structure with different fields and the field also have lots of values . can we access field by using value?
like we don't know anything about struct or field , we just have values in our hand so can we find which field contains certain value?

Réponse acceptée

KSSV
KSSV le 10 Oct 2022
S = struct ;
S.name = {'Tom' 'Dick' 'Harry'} ;
S.sex = {'M' 'M' 'F'} ;
S.Age = [16 17 18] ;
val = 'F' ;
fields = fieldnames(S) ;
for i = 1:length(fields)
vals = getfield(S,fields{i}) ;
[c,ia]= ismember(vals,val) ;
if any(c)
fprintf('given value is present in %s field\n',fields{i}) ;
end
end
given value is present in sex field
  5 commentaires
KSSV
KSSV le 12 Oct 2022
Yes you can save. Read about save
Urvashi
Urvashi le 12 Oct 2022
actually, i want to make a list of all the outcomes of if -condition and save it in variable.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming Utilities dans Help Center et File Exchange

Tags

Produits


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by