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
2
3
4
5
6
% lookup 1 level directory relative to current directory
ls ..
% lookup 2 level directory relative to current directory
ls ../..
% lookup 3 level directory relative to current directory
ls ../../..

You can also switch your current directory via the command cd

1
2
cd ../
cd ../../dir2

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
2
3
/codes/matlab/run/runmain.m  % current working directory
/codes/matlab/dir1/f2.m % f2.m directory
/codes/matlab/dir2/f3.m % f3.m 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function abspath = addmypath(filepath, relative_path)
% construct fullpath from relativepath relative to filepath
% N. CHU 2018.10.23


if nargin == 0
filepath = fileparts(mfilename('fullpath'));
relative_path = './../test/dir2';
end


%% split relative path
re_path_split = strsplit(relative_path, '/');
re_paths = re_path_split;
filepath_split = strsplit(filepath, '/');
file_paths = filepath_split;


%% backward according to relative path
for i = 1:length(re_path_split)
% remove './' case
if strcmp(re_path_split(i), '.')
re_paths(1) = [];
end
% remove '..' case and upward one level
if strcmp(re_path_split(i), '..')
re_paths(1) = [];
file_paths(end) = [];
end
end


%% construct full path with relative path
re_path = strjoin(re_paths, '/');
file_path = strjoin(file_paths, '/');
abspath = fullfile(file_path, re_path);

% add absolute path to search directory
addpath(abspath);


%% check path finding result
if nargin == 0
disp(filepath);
disp(relative_path);
disp(abspath);
end


end

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

[1] https://stackoverflow.com/questions/6091064/matlab-is-there-a-way-to-get-the-path-of-the-current-script

[2] https://stackoverflow.com/questions/38802539/find-location-of-current-script-mlx-file-in-matlab

[3] https://www.mathworks.com/help/matlab/ref/strsplit.html

[4] https://www.mathworks.com/help/matlab/ref/strjoin.html