Ben Vincent
Ben Vincent

Reputation: 381

matlab: cannot import package

Probably a basic mistake, but the cause is eluding me. I am trying to import a package, but I get an error saying it cannot be found or imported.

First I set the current directory to the parent directory of the package, and this does not work.

Second, the docs say that the parent folder of the package must be added to the matlab path. I tried this, and still no luck.

enter image description here

It is not due to using plot as the package name as I get the same error when trying to import analysis.

What I can do is to import using: import plot.* or import analyse.* and then go on to use the functions in the packages, but I want to use the namespaces (i.e. not use .*).

Edit I'm having this problem on both versions I have installed: 2015b and 2016a.

Upvotes: 1

Views: 754

Answers (1)

Dan
Dan

Reputation: 45752

The answer is that, somewhat counterintuitively, you don't need to call import at all. The docs state that

The parent of the top-level package folder must be on the MATLAB path.

Which is what your addpath(pwd) does and then state that (emphasis is mine):

All references to packages, functions, and classes in the package must use the package name prefix, unless you import the package.

Meaning at this stage you should be able to call

analyse.testFunc

If you were to import analyse.testFunc you would then be able to call testFunc without prefacing it with the namespace but since you want to retain the namespace the answer is to not call import at all.

Upvotes: 2

Related Questions