1
votes

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.

1
Post a link to the package? With regards to your use of addpath (a) it will only be for the current session. Use savepath to make it permanent and (b) it won't include subfolders, use genpath for that like this addpath(genpath(pwd)) (from the addpath docs) - Dan
but how do you want to use it? I ask because you could also just make it like a toolbox i.e. leave off the + in the folder names and use the addpath(genpath... syntax depending on your usecase - Dan
Unless I've misunderstood, I still don't think you need to use import unless you only want to get part of you package into your path. From these docs I think your current addpath(pwd) should work and you should be able to use analyse.testFunc. The docs state All references to packages, functions, and classes in the package must use the package name prefix, unless you import the package. - Dan
At the bottom right of the documentation there is a "was this helpful" section, click no and you can write to them to explain what your confusion was. It's the one advantage of MATLAB being proprietary, they do respond to feedback about the docs. - Dan

1 Answers

2
votes

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.