can i access field by using values in structure?

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?

 채택된 답변

KSSV
KSSV 2022년 10월 10일
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

Urvashi
Urvashi 2022년 10월 10일
i can't understand the last ffifth line
val = 'F' ;
It is the value in which I am interested to find whether present in the structure.
Urvashi
Urvashi 2022년 10월 12일
can i save the outcomes of the above code?
KSSV
KSSV 2022년 10월 12일
Yes you can save. Read about save
Urvashi
Urvashi 2022년 10월 12일
actually, i want to make a list of all the outcomes of if -condition and save it in variable.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming Utilities에 대해 자세히 알아보기

제품

릴리스

R2016b

태그

질문:

2022년 10월 10일

댓글:

2022년 10월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by