Matlab users can package code projects as toolboxes and/or packages. The packages format works for ancient versions of Matlab, before even R2006a as well as GNU Octave. The toolbox format works for Matlab ≥ R2016a and not GNU Octave. The packages format brings benefits to toolboxes as well.
A key issue with Matlab vs. Python arise from that Matlab users often add many paths for their project. If any function names clash, there can be unexpected behavior as it’s not immediately clear which function is being used without further investigation of path ordering. As in Python and other languages, there is considerable benefit for using a package format where the function names are specified in their namespace.
To enable Matlab package format, we explain by example. Suppose a project directory structure is like:
myproj utils mem1.m conversion deg1.m sys disk1.m
To use these functions, the end users do:
This is where the namespace can have clashes, and with large projects it’s not clear where a function is without further introspection.
To make this project a Matlab / Octave package, change the subdirectories containing .m files to start with a “+” plus symbol:
myproj +utils mem1.m +conversion deg1.m +sys disk1.m
Now the end users will simply:
and then access specific functions like:
Then multiple subdirectories can have the same function name without clashing in the Matlab namespace. Support the function “mem1” is used frequently in another function. To avoid typing the fully resolved function name each time, use the import statement:
function myfunc() import myproj.utils.mem1 mem1(arg1) mem1(arg2)
Matlab toolbox .mltbx
Matlab .mltbx toolboxes became available in R2016a. The Matlab-proprietary toolbox format also allows end users to create their own packages containing code, examples and even graphical Apps. In effect .mltbx provides metadata and adds the package to the bottom of Matlab path upon installation. The installation directory is under (system specific)/MathWorks/MATLAB Add-Ons/Toolboxes/packageName. Whether or not the project uses .mltbx, the namespace of the project is kept cleaner by using a Matlab package layout.