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.

  1. floating point values! careful!! never compare floating point value val==0.3! abs(val-0.3)<10^-8 or similar. read more here.

  2. you using structfun wrong. function needs 2 arguments, , passing 1! however, structfun apply 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

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -