MATLAB: How to add all subfolders in the matlab path

path subfolder

For example, my matlab folder 'current_use' is under the directory user/example/matlab/current_use and under 'current_use' folder there are 6 subfolders and each contains data files I need to test, i.e., my matlab script under folder 'current_use' will call all data under the subfolders which are inside folder 'current_use'
What I am doing now is each time tell matlab the path of each my subfolder, i.e., inside my script I have a comment to tell matlab to look the path 'user/example/matlab/current_use'.
It works. However, now I need to upload my code to the school server to run it. But after I upload everything onto school server, the path 'user/example/matlab/current_use' will no longer by correct. i.e., the part 'user/example/matlab/' is changed.
So I am wondering, is there a way that I can let matlab to add all subfolders under current folder without tell it where is the current folder? or, is there a comment that matlab can use to obtain the path for the current folder?
Thank you!

Best Answer

This should work regardless if your current folder is the one that contains your m-file, or not.
% Determine where your m-file's folder is.
folder = fileparts(which(mfilename));
% Add that folder plus all subfolders to the path.
addpath(genpath(folder));