MATLAB Error - Local function name must be different from the script name.
134 次查看(过去 30 天)
显示 更早的评论
Hi, I am using the following code (saved as c_objective.m) for defining the objective function used with gamultiobj. I am getting an error:
"Error: File: c_objective.m Line: 3 Column: 14
Local function name must be different from the script name."
Please help me resolve this. Thank you.
% Define the multi-objective function for Pareto search
global intcon f Q nmsi M S I
function F = c_objective(x)
F(1) = -(f'*x+x'*Q*x);
tempx=[nmsi x];
for m=1:M
for s=1:S
for i=1:I
index=tempx(find(tempx(:,2)==m & tempx(:,3)==s & tempx(:,4)==i));
dvstorage(m,s,i)=tempx(index,5);
end
end
end
entropy_simple=0;
for i = 1 : I % for each SKU
K1=0;
for m= 1: M % For each Aisle
nim=sum(dvstorage(m,:,i)); % sum for every I, for every m SKU stock(all ones)
K1=K1+nim*log(nim+0.00001);
end
K1=-K1;
Ni=sum(sum(dvstorage(:,:,i)));
K2=Ni*log(Ni+0.0000001);
entropy_simple=entropy_simple+K1+K2;
end
F(2) = -entropy_simple;
end
0 个评论
采纳的回答
Bruno Luong
2024-9-22
编辑:Bruno Luong
2024-9-22
Perhaps swap those two fitst lines in your code file
function F = c_objective(x)
global intcon f Q nmsi M S I
...
Warning: using global variables is NOT recommended practice
4 个评论
Steven Lord
2024-9-22
I was not sure whether I can pass more than just the decision variable vector (x) to the objective function.
Yes, you can. This documentation page describes two approaches for doing so that don't involve global variables.
更多回答(1 个)
Venkat Siddarth Reddy
2024-9-22
编辑:Venkat Siddarth Reddy
2024-9-22
Hi Nilendra,
The error message indicates that the local function has same function name as the filename of a MATLAB script in which it is defined. This is not allowed in MATLAB, to fix the issue please rename either the function or the MATLAB script.
In this case please change the name from "c_objective" either for the local function or the MATLAB script.
I hope it helps!
3 个评论
Stephen23
2024-9-22
_"The script is a standalone function file with the same name c_objective.m. It is not defined in any other MATLAB file."_
No, you do not have a function file. By adding code outside of the function definition you created a script (and a local function):
"There is one possibility. My declaring the variables outside the function code, was making this function a local function, embedded in another script. Perhaps that's why declaring the global variables after the function name"
Yes. Any code outside of a function definition turns the file into a script.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!