unable to call function in script

12 vues (au cours des 30 derniers jours)
Hoe-Phuan Ng
Hoe-Phuan Ng le 31 Août 2017
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.

Réponse acceptée

Hoe-Phuan Ng
Hoe-Phuan Ng le 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 commentaire
Cam Salzberger
Cam Salzberger le 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.

Connectez-vous pour commenter.

Plus de réponses (1)

Cam Salzberger
Cam Salzberger le 31 Août 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 commentaires
Steven Lord
Steven Lord le 31 Août 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 le 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB Report Generator dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by