为什么 MATLAB 抱怨它“禁止使用相同的名称......作为函数和变量”?
嘿伙计们。好的。我已经完成了这个编码。但好像有错误。谁能向我解释为什么会出现错误?这是编码:
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 if size(cost,1) == 1
==> 中的错误gui_mainfcn 位于 96 feval(varargin{:});
错误==> fyp_编辑器 42 岁 gui_mainfcn(gui_State, varargin{:});
错误==> @(hObject,eventdata)fyp_editor('Mybutton_Callback',hObject,eventdata,guidata(hObject))
???评估 uicontrol 回调时出错
Hye guys. Okay. I have done this coding. But it seems have error. Can anyone explain to me why there is an error? This is the coding:
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');
This is the error occured:
??? At compilation, "cost" was determined to be a variable and this
variable is uninitialized. "cost" is also a function name and previous versions
of MATLAB would have called the function.
However, MATLAB 7 forbids the use of the same name in the same
context as both a function and a variable.
Error in ==> fyp_editor>Mybutton_Callback at 131
if size(cost,1) == 1
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> fyp_editor at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)fyp_editor('Mybutton_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您遇到的可能问题是
costA
不为空,因此您的嵌套代码没有会被评估,并且变量cost
永远不会被评估在调用行if size(cost,1) == 1
之前初始化为任何内容。您可能想要对 嵌套 if 语句 进行如下排序:您遇到的异常错误是
cost
在您的代码中显示为变量,但似乎有一个函数,其名称成本
也是如此。当您定义与函数同名的变量时,变量优先,并且每当在任何计算中使用该名称时,都会使用变量代替函数。即使
cost
没有初始化为您的条件中的任何内容,MATLAB 仍然会识别它可能是您函数中的变量,因此(在 MATLAB 7 或更高版本中)它不会尝试调用函数cost
。显然,在旧版本中,如果同名变量未初始化,MATLAB 将调用隐藏函数。The likely problem you're having is that
costA
is not empty, so none of your nested code gets evaluated and the variablecost
will never be initialized to anything before you call the lineif size(cost,1) == 1
. You probably want to order your nested if statements something like this:The explanation for the unusual error you're getting is that
cost
appears in your code as a variable, but there appears to be a function with the namecost
as well. When you define a variable with the same name as a function, the variable takes precedence and will be used instead of the function whenever that name is used in any computations.Even though
cost
doesn't get initialized to anything in your condition, MATLAB still recognizes that it could be a variable in your function and thus (in MATLAB 7 or newer) it doesn't try to call the functioncost
. Apparently, in older versions MATLAB would call the shadowed function in the event that a variable by the same name is uninitialized.看起来错误表明
cost
变量是保留字。这意味着您不能将此名称用作变量。尝试将
cost
重命名为其他名称,例如finalCost
。It looks like the error is saying that the
cost
variable is a reserved word. This means you can't use this name as a variable.Try renaming
cost
to something else, likefinalCost
.