问题描述
如此处所述,软件包中的功能如下:以及类中的静态方法,仍然需要对 each 函数使用packagename.functionname
语法或import packagename.*
(因为导入是函数工作区的一部分,而不是全局的).这意味着以后更改程序包/类名称可能会变得很乏味.
As noted here, functions in packages, as well as static methods in classes, still need to use a packagename.functionname
syntax or import packagename.*
for each function (since the imports are part of the function workspace and not global). This means that changing the package/class name later on can become a tedious nuisance.
推荐答案
所以...这是否还要求importthis也要导入?还是importthis是您始终拥有的功能?
So... doesn't this require importthis to also be imported? Or is importthis a function you always have in your path?
在每个函数的顶部粘贴一个"import this"块似乎并不复杂,然后您不必担心importthis在您的路径中.我倾向于觉得依靠路径是危险的.
It seems hardly more complex to just paste an "import this" block with this at the top of each function, and then you don't have to worry about importthis being in your path. I tend to feel that reliance on path is dangerous.
%% Import own package
[~, pkgdir] = fileparts(fileparts(mfilename('fullpath')));
import([pkgdir(2:end) '.*']);
您甚至可以将其放在try/catch块中,以确保它在程序包目录中,并确定是否存在该程序.
You can even put it in a try/catch block to make sure it's in a package directory, and decide what to do if it's not.
%% Import own package
try
[~, pkgdir] = fileparts(fileparts(mfilename('fullpath')));
import([pkgdir(2:end)'.*']);
catch err
if ~strcmp(err.identifier,'MATLAB:UndefinedFunction'), rethrow(err); end
end
这篇关于在MATLAB中可以进行自引用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!