无法定义具有重名 "DoBenchmark" 的奇怪函数,因为该函数仅在一处定义

Strange Function with duplicate name "DoBenchmark" cannot be defined as the function is defined only at one place

(使用最新的 windows10 下的 Matlab 2018b。)我有一个包含 DoStuff.m 的文件夹 Folder,其代码为:

%addpath('./SubFolder/SubSubFolder'); // SubSubFolder contains mex file defining myFunction used below
%close all;

function [res] = DoStuff(param) % Function has same name as the script defining it

    res = myFunction(param)

end

其中 myFunction'./SubFolder/SubSubFolder' 中包含的 mexw64 文件中定义。

自然而然地,在 Matlab 的 GUI 中(在文件夹 Folder 中)执行函数 DoStuff(param) 会抛出以下错误:

'myFunction' is not found in the current folder or on the MATLAB path, but exists in ...

... 在 './SubFolder/SubSubFolder' 中。高超。因此,我删除了 DoStuff.m 第一行中的 % 并在 Matlab 的 GUI 中(在文件夹 Folder 中)重新执行函数 DoStuff(param) 并得到以下错误:

Function with duplicate name "DoStuff" cannot be defined.

奇怪,因为 DoStuff 只在一处定义:在 DoStuff.m 脚本中。 (由 Folder 中的 Matlab which -all DoStuff 确认。)

备注。 在 Matlab2018b 中,可以 在名为 toto.m 的脚本中定义一个名为 toto 的函数, Matlab 不会有任何问题。所以我的问题与相同的命名无关。它与添加 addpath 行有关,但我不知道如何添加。确认这种感觉:将 res = myFunction(param) 行替换为 res = 1 并取消注释 addpath 也会导致命名错误。

如果函数在脚本文件中,m-file 的名称必须与函数的名称不同。即,如果 DoStuff 不是函数文件,则您的 m-file 名称不能是 DoStuff.mDoStuff.m 应该是这样的函数文件:

function [res] = DoStuff(param) % Note that there is no executable line before this
res = myFunction(param)
end

或者您应该重命名您的函数或 m-file。

假设您重命名 m-file,您可以像这样使用它:

addpath('./SubFolder/SubSubFolder'); %SubSubFolder contains mex file defining myFunction
close all;

res = DoStuff(param); %Calling the function

function [res] = DoStuff(param)
res = myFunction(param)
end