unable to call function in script

11 views (last 30 days)
I am using Win 10. I have included the folder where my scripts are via Set Path.
Here is an example:
In file InitializeNetwork.m, I have this function:
function nn = InitializeNetwork(architecture) % line #10
... ...
end
================================
From matlab R017a command line, I did these:
>> architecture=[336 100 20];
>> nn = InitializeNetwork(architecture);
Error: File: InitializeNetwork.m Line: 10 Column: 15
Function with duplicate name "InitializeNetwork" cannot be defined.
======================================
Please advice. Thank you.

Accepted Answer

Hoe-Phuan Ng
Hoe-Phuan Ng on 1 Sep 2017
Answer to self:
The error was due to inserted test code at begin of file in the same script which calls that function. It appears one cannot call the function within the same script.
Error also occurs when you place code below the function you want to call.
Thank you everybody for helping.
  1 Comment
Cam Salzberger
Cam Salzberger on 3 Sep 2017
You wouldn't happen to be using R2016a or an earlier release? Defining functions within a script is only supported in R2016b and later.

Sign in to comment.

More Answers (1)

Cam Salzberger
Cam Salzberger on 31 Aug 2017
Sounds like you have multiple functions in the same file named InitializeNetwork. Is there another function with that same name at line 1?
Here's how I reproduced the error:
function nn = InitializeNetwork(architecture)
nn = 10;
end
function nn = InitializeNetwork(architecture)
nn = 3;
end
Calling it with this:
>> n = InitializeNetwork(1)
Error: File: InitializeNetwork.m Line: 4 Column: 15
Function with duplicate name "InitializeNetwork" cannot be defined.
-Cam
  2 Comments
Steven Lord
Steven Lord on 31 Aug 2017
If you have the file open in the MATLAB Editor, there should be a red square in the upper-right corner and a red line somewhere below that. If you click on the red line(s) it should take you to the duplicate definition(s) in the file.
Hoe-Phuan Ng
Hoe-Phuan Ng on 1 Sep 2017
Thank you Cam and Steven. I clicked the red square at top right corner, and it jumped to the file (InitializeNetwork.m) where I defined the function InitializeNetwork().
I looked at all the files, there is not another script that has InitializeNetwork() defined.
I then removed all other files that use InitializeNetwork() from that folder.
I still have a problem and error message.
>> n = InitializeNetwork(1);
Error: File: InitializeNetwork.m Line: 10 Column: 15 Function with duplicate name "InitializeNetwork" cannot be defined.
I even restarted matlab.

Sign in to comment.

Categories

Find more on MATLAB Report Generator in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!