【问题标题】:Why does MATLAB complain it "forbids the use of the same name ... as both a function and a variable"?为什么 MATLAB 抱怨它“禁止使用相同的名称......作为函数和变量”?
【发布时间】:2011-03-10 20:13:20
【问题描述】:

大家好。好的。我已经完成了这个编码。但它似乎有错误。谁能向我解释为什么会出现错误?这是编码:

Hcurve = cell2mat(get(handles.Mytable3,'Data'));
costA = cell2mat(get(handles.Mytable1,'Data'));
cost1 = str2num(get(handles.input2_editText,'String'));
cost2 = str2num(get(handles.input3_editText,'String'));
cost3 = str2num(get(handles.input4_editText,'String'));
cost4 = str2num(get(handles.input5_editText,'String'));
limit = cell2mat(get(handles.Mytable2,'Data'));
Pdt = str2num(get(handles.input1_editText,'String'));

if isempty(costA)

    if size(Hcurve,1) == 2

        H1 = Hcurve(1,:)*cost1;
        H2 = Hcurve(2,:)*cost2;
        H = num2cell([H1;H2]);
        set(handles.Mytable1,'Data',H)
        cost = cell2mat(get(handles.Mytable1,'Data'));

    else if size(Hcurve,1) == 3 

        H1 = Hcurve(1,:)*cost1;
        H2 = Hcurve(2,:)*cost2;
        H3 = Hcurve(3,:)*cost3;
        H = num2cell([H1;H2;H3]);
        set(handles.Mytable1,'Data',H)
        cost = cell2mat(get(handles.Mytable1,'Data'));

    else if size(Hcurve,1) == 4 

        H1 = Hcurve(1,:)*cost1;
        H2 = Hcurve(2,:)*cost2;
        H3 = Hcurve(3,:)*cost3;
        H4 = Hcurve(3,:)*cost4;
        H = num2cell([H1;H2;H3;H4]);
        set(handles.Mytable1,'Data',H)
        cost = cell2mat(get(handles.Mytable1,'Data'));

        else 

        cost = costA;

        end
        end
    end
end


if size(cost,1) == 1

    set(handles.text8,'String','At Lease Two Generators');

这是发生的错误:

???在编译时,“成本”被确定为一个变量,这 变量未初始化。 “cost”也是函数名和以前的版本 MATLAB 会调用该函数。 但是,MATLAB 7 禁止在同一个文件中使用相同的名称 上下文既是函数又是变量。

==> fyp_editor>Mybutton_Callback 中的错误在 131 如果大小(成本,1)== 1

==> gui_mainfcn 中的错误为 96 feval(varargin{:});

==> fyp_editor 在 42 处出错 gui_mainfcn(gui_State, varargin{:});

==> 中的错误 @(hObject,eventdata)fyp_editor('Mybutton_Callback',hObject,eventdata,guidata(hObject))

???评估 uicontrol 回调时出错

【问题讨论】:

    标签: user-interface uitableview matlab matlab-guide


    【解决方案1】:

    您可能遇到的问题是 costA 不为空,因此 none 您的嵌套代码会被评估,并且变量 cost 在您调用线if size(cost,1) == 1。您可能想订购您的nested if statements 类似这样的东西:

    if isempty(costA)  %# If costA is empty, compute a value for cost
    
      if size(Hcurve,1) == 2
      ...
      elseif size(Hcurve,1) == 3
      ...
      elseif size(Hcurve,1) == 4
      ...
      end
    
    else
      cost = costA;  %# Will set cost equal to costA if it is not empty
    end
    

    对您遇到的异常错误的解释是,cost 在您的代码中显示为一个变量,但似乎有一个名称为函数 cost 也是如此。当您定义一个与函数同名的变量时,variable takes precedence 将在任何计算中使用该名称来代替函数。

    即使 cost 在您的条件下没有被初始化为任何东西,MATLAB 仍然认识到它可能是您的函数中的一个变量,因此(在 MATLAB 7 或更高版本中)它不会t 尝试调用 函数 cost。显然,在旧版本中,如果同名变量未初始化,MATLAB 会调用阴影函数。

    【讨论】:

      【解决方案2】:

      看起来错误是说cost 变量是保留字。这意味着您不能将此名称用作变量。

      尝试将 cost 重命名为其他名称,例如 finalCost

      【讨论】:

      • cost 不是 MATLAB keyword一个内置函数cost,但这不是问题,因为 MATLAB 意识到他想将其用作变量,除非他在未初始化时使用它。跨度>
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 1970-01-01
      相关资源
      最近更新 更多