Search path is a very important concept in Matlab programming. Your personal codes can only be run when it is added to the search path.
Concept of directory
Current directory
The main script you shall execute should be set as working directory. This working directory is current direction. This concept is very easy to understand for those who are familiar with linux. However, windows users may get confused by this concept. You need to practice to understand it. You current directory can be view in matlab or linux with this command.
1 | pwd |
Relative directory
Relative directory refers to the directory relative to current directory. You can view relative directory with these command:
1 | % lookup 1 level directory relative to current directory |
You can also switch your current directory via the command cd
1 | cd ../ |
Add search path
If you wish to use a matlab module that are not matlab build-in function, you need first to add its directory to matlab search path. The concept of path is equal to the concept of directory. They are the same thing.
Add absolute path
This can be down directly with addpath() command.
1 | addpath('/home/user/nan/codes/matlab/plot/'); |
However, add absolute path in your code will cause trouble when your code is used on a second machine where the absolute to your code is usually different to your first machine.
Add relative path
Matlab will search the relative path based on the current directory. In this way, you can call functions defined under that directory. This could also be down with addpath function.
1 | addpath('../../matlab/plot') |
Add relative path in script outside current directory
Sometimes, you have to call for a function not in current directory. And this function will need to addpath of directory relative to itself. eg. you have code structure like this:
1 | /codes/matlab/run/runmain.m % current working directory |
So simply calling function f3 from function f2 will cause errors. In order to solve this problem, I wrote a small function to calculate the absolute path of a directory relative to a give path. This function is named as addmypath().
addmypath()
Addmypath is a small script to add relative path within subscripts that are not located in current directory. During the building up of this script, some good references[1, 2, 3, 4] are looked over.
1 | function abspath = addmypath(filepath, relative_path) |
Currently, I have not found a good solution to find the script location from a subfunction. So this function addmypath() can only be used like this:
1 | addmypath(fileparts(mfilename('fullpath')), '../dir2'); |
And you must make sure that you have addpath to addmypath.m directory in the first place. In this line, mfilename(‘fullpath’) will return the file name with absolute full path of this file. fileparts() will remove the filename from this string and leave the path direction along.
Best approach with fullfile()
The function addmypath() has the problem to add itself to the search path. After some discussion with my friend, he suggests that the best approach is to replace addmypath() with matlab build-in function fullfile(). Thus the code can be written like this :
1 | addpath(fullfile(mfilename('fullpath')), '../dir2'); |
Reference
[2] https://stackoverflow.com/questions/38802539/find-location-of-current-script-mlx-file-in-matlab