can i access field by using values in structure?

2 次查看(过去 30 天)
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-12
actually, i want to make a list of all the outcomes of if -condition and save it in variable.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming Utilities 的更多信息

标签

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by