How do I perform an import for the entire class in matlab?

可紊 提交于 2019-12-10 14:18:41

问题


I have a class that uses other classes from another package in multiple functions. In order to do this, I current have to import the package in each function:

classdef foo
    properties
        bar
    end
    methods
        function self = foo()
            foo.bar = 1;
        end

        function fun1(foo)
            import pkg.FooClass;
            val = pkg.FooClass(foo.bar);
        end
        function fun2(foo)
            import pkg.FooClass;
            val = FooClass.fun(foo.bar);
        end
    end
end

Is there a way to import packages for the entire class? I'm looking for something similar to other languages:

classdef foo
    import pkg.FooClass;
    properties
        bar
    end
    methods
        function self = foo()
            foo.bar = 1;
        end

        function fun1(foo)
            val = pkg.FooClass(foo.bar);
        end
        function fun2(foo)
            val = FooClass.fun(foo.bar);
        end
    end
end

回答1:


Unfortunately, the doc page says that:

The import function only affects the import list of the function within which it is used.

So you will either have to specify the full qualified name everywhere, or do an import in each function.



来源:https://stackoverflow.com/questions/17050167/how-do-i-perform-an-import-for-the-entire-class-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!