When working in Matlab, I usually create small projects that rely on different toolboxes. In order not to add all the toolboxes I want to load in the $MATLAB_PATH environment variable, I wrote a couple of functions that help me including what I need when needed.
Example
Let’s imagine we are working on two projects:
- Project1 in ~/tmp/project1
- Project2 in ~/tmp/project2
Let’s also imagine that Project2 requires alls the functions of Project1.
By using mtpath, we can then write an initialization file for Project1 (~/tmp/project1/project1_init.m):
1 2 3 4 5 6 7 8 9 10 11 12 | % Init file for Project1: ~/tmp/project1/project1_init.m mtpath_include('$CNBI_ROOT/Projects/Classification/Gaussian'); mtpath_include('$CNBI_ROOT/Projects/FeatureSelection/CVA'); mtpath_include('$CNBI_ROOT/Projects/gdfmatlab'); mtpath_include('$CNBI_ROOT/Thirdparty/matlab/gkde'); mtpath_include('$CNBI_ROOT/Thirdparty/matlab/biosig'); mtpath_include('$CNBI_ROOT/Thirdparty/matlab/biosig/t200'); mtpath_include('$CNBI_ROOT/Thirdparty/matlab/biosig/t250'); mtpath_include('$CNBI_ROOT/Thirdparty/matlab/biosig/t300'); mtpath_include('$CNBI_ROOT/Thirdparty/matlab/biosig/t400'); mtpath_include('$CNBI_ROOT/Thirdparty/matlab/biosig/t490'); mtpath_include('$CNBI_ROOT/Thirdparty/matlab/biosig/t500'); |
All the lines above simply add all the toolboxes to the Matlab path. We said that Project2 requires Project1 to work. If Project1 did not need any external toolbox, we could simply do:
>> addpath('~/tmp/project1/');
Since this is not the case, we can use mtpath_include.m to add Project1 to the search path. mtpath_include.m will discover that a file called ~/tmp/project1/project1_init.m exists, and it will execute it, adding all the dependencies of Project1 automatically.
We could use an initialization file for Project2 as well (~/tmp/project2/project2_init.m):
1 2 | % Init file for Project2: ~/tmp/project2/project2_init.m mtpath_include('~/tmp/project1/'); |
mtpath_init.m can be used to call automatically the *_init.m file in the current directory:
>> cd ~/tmp/project2 >> mtpath_init [mtpath_include] Adding: /home/mtavella/project1 [mtpath_include] Adding: /tmp/cnbi-root/trunk/Projects/Classification/Gaussian [mtpath_include] Adding: /tmp/cnbi-root/trunk/Projects/FeatureSelection/CVA [mtpath_include] Adding: /tmp/cnbi-root/trunk/Projects/gdfmatlab [mtpath_include] Adding: /tmp/cnbi-root/trunk/Thirdparty/matlab/gkde [mtpath_include] Adding: /tmp/cnbi-root/trunk/Thirdparty/matlab/biosig [mtpath_include] Adding: /tmp/cnbi-root/trunk/Thirdparty/matlab/biosig/t200 [mtpath_include] Adding: /tmp/cnbi-root/trunk/Thirdparty/matlab/biosig/t250 [mtpath_include] Adding: /tmp/cnbi-root/trunk/Thirdparty/matlab/biosig/t300 [mtpath_include] Adding: /tmp/cnbi-root/trunk/Thirdparty/matlab/biosig/t400 [mtpath_include] Adding: /tmp/cnbi-root/trunk/Thirdparty/matlab/biosig/t490 [mtpath_include] Adding: /tmp/cnbi-root/trunk/Thirdparty/matlab/biosig/t500
This is a simple example of how mtpath could be used to recursively include toolboxes and their dependencies. The mtpath_include.m function can also add subdirectories recursively. Finally, it never adds duplicate folders to the Matlab search path.
Download
mtpath-0.1.0.tar.gz2 – 2010-08-23