Can I have subdirectories in a +package directory?
19 ビュー (過去 30 日間)
古いコメントを表示
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?
2 件のコメント
Michael Häußler
2018 年 7 月 17 日
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 ;)
Jim Svensson
2019 年 5 月 14 日
The mechanism you want does not exist. But you achieve a similar thing by putting the differrent folder groups first and multiple packages of the same name inside, like so:
<root>/utils/+pkg_foo/find_data.m
<root>/apps/+pkg_foo/plot_data.m
addpath/genpath(<root>))
d = pkg_foo.find_data();
pkg_foo.plot_data(d);
採用された回答
その他の回答 (2 件)
Philip Joergensen
2018 年 11 月 15 日
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 件のコメント
Philip Joergensen
2018 年 11 月 15 日
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.
Michael Häußler
2018 年 11 月 15 日
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:
参考
カテゴリ
Help Center および File Exchange で File Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!