Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Can Matlab packages have subdirectories?
I'd like to organize my Matlab packages into folders. However, if I try to lay out my code like this
+MyPackage
|---foo.m
|---SubDir
|---bar.m
then I'm unable to reach bar. For example, if the contents of foo.m are
function foo
disp("foo")
bar
end
and the contents of bar.m are
function bar
disp("bar")
end
then running MyPackage.foo
outputs
foo
Error using bar
Not enough input arguments.
Error in MyPackage.foo (line 3)
bar
Trying to add SubDir with addpath
gives a warning that package directories are not allowed in the MATLAB path. Does everything in a Matlab package really need to be within a single directory, or is there a way to organize packages with subdirectories?
1 answer
Well, minutes after posting my question, I found the disappointing answer: no.
Original answer found on https://www.mathworks.com/matlabcentral/answers/184191-can-i-have-subdirectories-in-a-package-directory from Jim Svensson.
His suggested workaround is to have multiple packages with the same name, but placed in different directories, like so:
<root>/utils/+pkg_foo/find_data.m
<root>/apps/+pkg_foo/plot_data.m
0 comment threads