MATLAB Error - Local function name must be different from the script name.
127 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Nilendra Singh Pawar
le 22 Sep 2024
Commenté : Nilendra Singh Pawar
le 23 Sep 2024
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 commentaires
Réponse acceptée
Bruno Luong
le 22 Sep 2024
Modifié(e) : Bruno Luong
le 22 Sep 2024
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 commentaires
Steven Lord
le 22 Sep 2024
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.
Plus de réponses (1)
Venkat Siddarth Reddy
le 22 Sep 2024
Modifié(e) : Venkat Siddarth Reddy
le 22 Sep 2024
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 commentaires
Stephen23
le 22 Sep 2024
_"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.
Voir également
Catégories
En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!