MATLAB Answers

2

Can I have subdirectories in a +package directory?

Asked by Richard Crozier on 20 Mar 2015
Latest activity Answered by Richard Crozier on 19 Nov 2018 at 8:40
I would like to know if it is possible to add subdirectories to a matlab package directory (denoted by a preceeding '+' in the directory name). Often I want to split up my functions within a package into logical units in directories, but without needing them to each have their own subnamespace. In other words, is it possible to have:
/+pkg/toplevelfun.m
/+pkg/subdir/lowerlevelfun.m
and have both called in my matlab code as:
pkg.toplevelfun
pkg.lowerlevelfun
My tests indicate this is not possible (at least in 2014a). Is this impossible, and if so, is there some technical reason it could not be implemented by TMW?

  1 Comment

I am looking for the same feature as you! This would give me the opportunity to make my packages a lot clearer in their arrangement.
Come on Matlab Team ;)

Sign in to comment.

3 Answers

Answer by Richard Crozier on 19 Nov 2018 at 8:40
 Accepted Answer

The answer is no, you cannot have subdirectories in a package directory.

  0 Comments

Sign in to comment.


Answer by Philip Joergensen on 15 Nov 2018 at 10:33

Not sure why it does not make sense to place both functions in the root of the package in the case you describe.
I figured out, if you do not mind having a namespace for each subdirectory, you can have subpackages in your package:
/+pkg/toplevelfun.m
/+pkg/+subpkg/lowerlevelfun.m
And then calling the lowerlevelfun would be
pkg.subpkg.lowerlevelfun(...)
Which seems to make good sense in this use case, as it is easy to group functions into easy to remember and meaningful namespaces.

  3 Comments

Yes, I'm aware of nested namespaces, this isn't what I want, thanks.
As to why, it is my preference for organisation, but without an exploding number of namespaces.
That is reasonable, each to their own.
It just does not seem to me to be the best practice/most logical step to group functions in a structure not used consistently.
Organizing a function into
+pkg/<some_path_to_function_dir>/lowerlevelfunc.m
called by
pkg.lowerlevelfunc(...)
makes it take effort to figure out the location of the function. Also, it begs the question how should functions with the same name be handled.
Do you mean effort for the user? The user can just do
which('pkg.lowerlevelfunc')

Sign in to comment.


Answer by Michael Häußler on 15 Nov 2018 at 11:43

There is also the possibility to use private folders within the package. The functions/classes within that folder are however only visible to the files within the package....
Still not what you want exactly, but I actually use that a lot now to have small "helper functions" within the package organized at a different place:

  1 Comment

Thanks, yes, I'm also aware of this, but this doesn't do what I want at all, since I want the functions to be available through the package namespace. Even if it did do something like what I wanted, as there can naturally only be one private directory, called 'private' in a package directory, it doesn't really help with organisation in any way.

Sign in to comment.