2015-09-21 22 views
0

我有以下的嵌套結構:GET字段名

孔1x200結構,直徑1×12結構,其具有以下領域:POS,FREQ1,fre12

即:

hole(1 to 200).diam(1 to 12).pos 
          .freq1 
          .freq2 

從值(freq1freq2),我想要獲得結構的字段名稱。所以我需要找到與freq1和freq2匹配的值並顯示字段名。

我試圖使用structfun爲了將函數應用到每個字段。

[struct.field]=structfun(@(x) find(x.freq1==27.059783995484867 & freq2==76.468355874897000)) 

但我想我在代碼寫錯了。

此外,我創建一個匿名機能的研究,但我有以下錯誤:

'Error using structfun/Inputs to STRUCTFUN must be scalar structures'

。如何過,當我覈實,如果結構的具體值是標量,我有一個積極的answerd:

[email protected](yourarray,desiredvalue) yourarray==desiredvalue; 
%//Apply function to each field of scalar structure, it SCALAR?? 
desiredfieldindex=myfun(structfun(@(x) x,hole),26.697046257785030) 
desiredFieldName=fNames(desiredFieldIndex) 

我不知道:isscalar(hole(130).diam(10))

我使用這個腳本相信的時候我更接近解決方案我處於適當的路徑,或者我應該利用函數find。此外,我不知道如何實施它。

回答

1

幾件事情。

  1. FLOATING POINT VALUES!小心!切勿將浮點值與val==0.3進行比較!做abs(val-0.3)<10^-8或類似的東西。 Read more here

  2. 您正在使用structfun錯誤。該函數需要2個參數,而你只是傳遞1!但是,structfun將對每個字段應用一個函數,因此您在這個意義上並沒有使用它。讓我們來看一個例子

例如:

a.hithere=5; 
a.notthis=3; 
[email protected](x)x==5; 
[isfive]=structfun(fun,a) 


isfive = 

    1 
    0 

正如你所看到的,它適用的功能,他們每個人。如果您嘗試將功能更改爲[email protected](x)x.hithere==5,您將收到錯誤消息!由於該功能適用​​於每個字段,因此不存在x.hithere.hitherex.notthis.hithere

如果你想檢查兩個值,你需要獨立檢查它們,分別撥打structfun或避開structfun,然後只做一個for循環。

+0

10^8應該是10^-8 – Navan

+0

我一直在嘗試一些例子,當我將你的邏輯實現到我的程序中時,它不起作用。我的情況如下 –

+0

@NicolasT。 「它不工作」?爲什麼它不起作用?因爲所問的問題,作品。 –