Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 204
James Mertz Avatar asked Jun 11 '13 17:06

James Mertz


People also ask

How do I import a library into MATLAB?

In the MATLAB Current Folder Browser, open the folder that contains mylib , then, in the MATLAB Toolstrip, click New Script. In the MATLAB Editor that opens, add this function, which specifies that the library mylib should appear in the Library Browser with the name My Library.

How do I use a class in another file in MATLAB?

To use multiple files for class definitions, put the class files in a folder having a name beginning with the @ character followed by the name of the class (this is called a class folder).

How do I open a class in MATLAB?

Open the Class Diagram Viewer MATLAB toolstrip: On the Apps tab, under MATLAB, click the app icon. Current Folder browser: Right-click on a class file or a folder that contains one or more class files and select View Class Diagram .


1 Answers

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.

like image 170
Amro Avatar answered Nov 15 '22 07:11

Amro