2015-03-03 34 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

传递主结构时不要使用任何引号。只需将结构名称直接传递给函数而不使用''。