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

查看:130
本文介绍了在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?

在每个函数的顶部粘贴一个"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天全站免登陆