unable to call function in script

10 次查看(过去 30 天)
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.

采纳的回答

Hoe-Phuan Ng
Hoe-Phuan Ng 2017-9-1
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 个评论
Cam Salzberger
Cam Salzberger 2017-9-3
You wouldn't happen to be using R2016a or an earlier release? Defining functions within a script is only supported in R2016b and later.

请先登录,再进行评论。

更多回答(1 个)

Cam Salzberger
Cam Salzberger 2017-8-31
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 个评论
Steven Lord
Steven Lord 2017-8-31
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 2017-9-1
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB Report Generator 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by