MATLAB Answers

function with duplicate name cannot be defined

53 views (last 30 days)
vinutha paravastu
vinutha paravastu on 25 Aug 2019
Commented: 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.....


madhan ravi
madhan ravi on 25 Aug 2019
Just change the name of your function to some other name?
vinutha paravastu
vinutha paravastu on 25 Aug 2019
i tried with different names...what ever name i use i am getting same error....

Sign in to comment.

Answers (1)

Steven Lord
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

vinutha paravastu
vinutha paravastu on 25 Aug 2019
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.

Translated by