MATLAB Answers


function with duplicate name cannot be defined

Asked by vinutha paravastu on 25 Aug 2019
Latest activity Commented on by vinutha paravastu on 25 Aug 2019
i wrote the following function and saved it as myobjective.m
Function i used
function obj= myobjective(p)
global c1meas c2meas c3meas c4meas c5meas
%simulate model
obj = sum(((c(:,1)-c1meas)).^2)
+ sum(((c(:,2)-c2meas)).^2)+sum(((c(:,2)-c3meas)).^2)+sum(((c(:,2)-c4meas)).^2)+sum(((c(:,2)-c5meas)).^2);
while running the function i am getting the following error ...
Error: File: myobjective.m Line: 2 Column: 15
Function with duplicate name "myobjective" cannot be defined.
i used all _my objective in command window and i got only 1 m file with that name...
pls help me guys.....


Just change the name of your function to some other name?
i tried with different names...what ever name i use i am getting same error....

Sign in to comment.

1 Answer

Answer by Steven Lord
on 25 Aug 2019

The error message states that the function definition is on the second line of the file, so I suspect you have other code on the first line. That makes that file a script file with a function in it, and according to the documentation "Script files cannot have the same name as a function in the file."
Rename either your script file or your function, or if you intended this file to be a function file rather than a script file remove the executable code on the first line of the file.

  1 Comment

thanku it solved my problem...
in the first line i wrote a comment with out % symbol might be it was taken as code...

Sign in to comment.