MATLAB Error - Local function name must be different from the script name.

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

 Risposta accettata

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 Commenti

Hi Bruno, It worked. Thank you for the help. Although I do not understand what was the problem in the way I had defined it earlier. Please let me know. Thanks in advance.
Regarding using global variables, I am using this function as an objective function in gamultiobj solver. I was not sure whether I can pass more than just the decision variable vector (x) to the objective function. Hence I used the other variables as global variables.
When you use "global" statement you must do it in a local workpace, where you intend yo use it. When you use global BEFORE the function declaration as your original code , MATLAB interprets your mfile as a script with separate workspace and the the function is SUBSUNCTION. Then it complains since the name of the script (mfile) and a subfunction could not be the same.
Anyway you need to know a little bit about function/script/declaration in mfile to avoid having errors with such details.
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.

Accedi per commentare.

Più risposte (1)

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 Commenti

Hi Venkat, The script is a standalone function file with the same name c_objective.m. It is not defined in any other MATLAB file.
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, (Ref. answer from @Bruno Luong) helped.
Thanks,
_"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.
Stephen, Thanks for confirming this.

Accedi per commentare.

Categorie

Scopri di più su Scripts in Centro assistenza e File Exchange

Prodotti

Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by