arrays - get fieldname of a data struct matlab -
i have following nested struct:
hole 1x200 struct, diam 1x12 struct, has following fields: pos, freq1, fre12
that is:
hole(1 200).diam(1 12).pos                             .freq1                             .freq2 from value (freq1 , freq2), obtain field name of struct. need find value matches freq1 , freq2 , show fieldname.
i tried use structfun in order apply function each field.
[struct.field]=structfun(@(x) find(x.freq1==27.059783995484867 & freq2==76.468355874897000)) but guess writing wrong on code.
also create anonymous fuction have following error:
'error using structfun / inputs structfun must scalar structures'
. how ever when verified if  specific value of struct scalar, have positive answerd: isscalar(hole(130).diam(10))
i belive more near solution using script:
myfun=@(yourarray,desiredvalue) yourarray==desiredvalue; %//apply function each field of scalar structure, scalar?? desiredfieldindex=myfun(structfun(@(x) x,hole),26.697046257785030)  desiredfieldname=fnames(desiredfieldindex) i don´t know if in rigth path, or should utilize function find. case don´t know how implement it.
couple of things.
- floating point values! careful!! never compare floating point value - val==0.3!- abs(val-0.3)<10^-8or similar. read more here.
- you using - structfunwrong. function needs 2 arguments, , passing 1! however,- structfunapply function each field not using rigth either in sense. lets see example
example:
a.hithere=5; a.notthis=3; fun=@(x)x==5; [isfive]=structfun(fun,a)   isfive =       1      0 as can see, applies function each of them. if try change function fun=@(x)x.hithere==5 error! function applied each field, , x.hithere.hithere or x.notthis.hithere not exist. 
if want check both values, need check them independently making 2 separated calls structfun or avoiding structfun , making loop.
Comments
Post a Comment