2015-03-03 30 views
0

這是一種跛腳,但我無法刪除此錯誤。我有一個函數,我提供了一個組件的名稱,它是結構add_strcut中的一個元素。未定義變量在MATLAB中的結構

所以add_strcut具有DATA_A,DATA_B等

和DATA_A具有字段 '賦'。並再次'頌'有'輸入'。

function bus_creator(component_name) 
if (isfield(add_strcut.(component_name),'ode')==1) 
    for loop_out=1:length(add_strcut.(component_name).ode.input) 
     for loop_in=1:length(fieldnames(add_strcut.(component_name).ode.input{loop_out,2})) 
     struct_name=add_strcut.(component_name).ode.input{loop_out,2}.(char(fieldnames(add_strcut.(component_name).ode.input{loop_in,2}))); 
     bus_creator_record(struct_name); 
     end 
    end 
end 

ofcourse這裏一邊撥打我公司供應的組件名稱爲 'DATA_A' 的功能。但第二行會引發錯誤。

錯誤是undefuned變量「add_strcut」或類「add_strcut.data_a」

即使當我使用F9來檢查它顯示我精的價值,但是當我調用此函數,它拋出我這個錯誤

更新

function bus_creator(main_component,component_name) 
    if (isfield(main_component.(component_name),'ode')==1) 
     for loop_out=1:length(main_component.(component_name).ode.input) 
      for loop_in=1:length(fieldnames(main_component.(component_name).ode.input{loop_out,2})) 
      struct_name=main_component.(component_name).ode.input{loop_out,2}.(char(fieldnames(main_component.(component_name).ode.input{loop_in,2}))); 
      bus_creator_record(struct_name); 
      end 
     end 
    end 
end 

這是更新的功能。現在我將提供main_component =「add_strcut」,但現在訪問它作爲一個變量我需要把main_component括號內,但如果我這樣做,那麼它拋出我的錯誤 在(main_component).(component_name)

它顯示的點之間的紅色和錯誤是matlab運算出現意外

+0

除非聲明爲全局,否則函數外部定義的變量不能在函數範圍外看到。所以解決的辦法是將'add_strcut'傳遞給函數(而不是'component_name')。另一種解決方法是刪除所有對'add_strcut'的調用,並只調用'component_name'。 – patrik 2015-03-03 08:48:52

+1

你從不向函數'bus_creator'傳遞'add_strcut'變量......所以確定它的範圍沒有定義... – CitizenInsane 2015-03-03 08:49:47

+0

什麼樣的錯誤? – Daniel 2015-03-03 09:01:02

回答

0

傳遞主結構時不要使用任何引號。只需將結構名稱直接傳遞給函數而不使用''。