Function with duplicate name cannot be defined

5 ビュー (過去 30 日間)
Aishwarya Bangalore Kumar
Aishwarya Bangalore Kumar 2017 年 12 月 16 日
編集済み: Sneha Thakur 2018 年 10 月 30 日
I have named the function name and .m file the same, and i am getting the error. If i dont it says it should be the same. someone help me

回答 (3 件)

Star Strider
Star Strider 2017 年 12 月 16 日
Rename your function to be different from the MATLAB function name.
For example, if the MATLAB function name is qwerty.m, rename yours as ‘myqwerty.m’, and call it appropriately in your code.
  2 件のコメント
Star Strider
Star Strider 2017 年 12 月 17 日
Aishwarya Bangalore Kumar’s Comment moved here —
Thank you
Star Strider
Star Strider 2017 年 12 月 17 日
If my Answer solved your problem, please Accept it!

サインインしてコメントする。


Image Analyst
Image Analyst 2017 年 12 月 17 日
The function name and the m-file name should be the same. For example, if it's called MyFunction(), then you should have an m-file called MyFunction.m and inside should be a function line followed by code:
function output = MyFunction(input1, input2)
% Code follows.....
If you're still getting that error message, try putting this on the command line:
>> which -all MyFunction
Where, of course, you replace MyFunction with the actual name of the function/m-file that you're using.
Please copy and paste ALL the red text that you are seeing, along with the results of the which statement, back here if you still need help.
  3 件のコメント
Walter Roberson
Walter Roberson 2018 年 4 月 18 日
Is your calc.m a script that contains inside it also a "function calc" ?
When you use the R2016b and later facility to put a function inside a script file (that is, a .m file that does not begin with "function" or "classdef") then the function(s) inside the script may not have the same name as the script itself.
Kendall Gibson
Kendall Gibson 2018 年 7 月 24 日
This was the issue with mine, thank you

サインインしてコメントする。


Mina Yazdani
Mina Yazdani 2018 年 9 月 23 日
編集済み: Walter Roberson 2018 年 9 月 23 日
Hi,I am doing a homework and i have to use a certain function name, which is "function [B]=even_index(A)". But i keep getting the same error. And i can't change the name cause the professor asked for this specific name. Could you help me?
Error: File: even_index.m Line: 4 Column: 14
Function with duplicate name "even_index" cannot be defined.
  3 件のコメント
Walter Roberson
Walter Roberson 2018 年 9 月 23 日
This will happen if you have a script named even_index that tries to define a function named even_index inside it.
When you define a function in a script, the function must have a different name than the script does.
Sneha Thakur
Sneha Thakur 2018 年 10 月 30 日
編集済み: Sneha Thakur 2018 年 10 月 30 日
Hi
I had similar issues. I had written few commands before the beginning of my function, which should not have been there, as
clc;clear all;
function y = MyFcn(x)
.....
end
After removing clc; clear all, it worked fine without this error.
Please check your function script and remove any duplicate name files if any in the folder.
thanks
Sneha

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by