在 MATLAB 中是否可以进行自引用? [英] Is self-reference possible in MATLAB?

查看:17
本文介绍了在 MATLAB 中是否可以进行自引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处所述,包中的函数为以及类中的静态方法,仍然需要为 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.

有什么方法可以做类似 import this.* 的操作,即一个包/类名不可知的方法来访问同一个包/类中的所有函数/静态方法?

Is there any way to do something like import this.*, i.e. a package/class name agnostic method to access all functions/static methods in the same package/class?

推荐答案

那么……这不是需要 importthis 也被导入吗?还是 importthis 是您一直在使用的功能?

So... doesn't this require importthis to also be imported? Or is importthis a function you always have in your path?

在每个函数的顶部粘贴一个带有 this 的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 中是否可以进行自引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆