How can I add all subfolders to my MATLAB path?

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!

 Accepted 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));

6 Comments

Is it different from using
addpath(genpath(pwd))
?
In most cases, yes. But not if your current folder is not the folder where the m-file is. For example if you were not in its folder and tried to run it and said "Add to path" instead of "Change folder" then they would not do the same thing.
Diaa
Diaa on 12 Nov 2019
Edited: Diaa on 12 Nov 2019
I am sorry but I don't quite understand your answer. It seems that both add the directoy of the running script folder to the path without changing the current folder. Am I right?
Furthermore, I would be grateful if you could help me on my recently asked relevant question .
Change Folder does not add the folder to the path. It sets the current folder to the folder of the m-file you're running. It is added to the path only when you're in that folder, but if you changed the current folder to something completely different, it wouldn't find it since it is not on the official "path" variable. It was only on the search path temporarily while the current folder was that.
For example: current folder is c:\a, and m-file is in c:\b. If you "change folder", current folder will be c:\b. It will find that file because the current folder is on the search path. But the official path was not changed. So if you change folder to c:\c, then type your m-file name in the command window, it will no longer find your m-file in folder c:\b because it was not added to the path with savepath().
Just adding a note for how to use this answer for live scripts:
simply replace mfilename with matlab.desktop.editor.getActiveFilename
as described here: https://www.mathworks.com/matlabcentral/answers/441522-determine-own-location-in-a-live-script#comment_1365606

Sign in to comment.

More Answers (2)

if you take a quick gander at the addpath documentation it'll show you
Add Folder and Its Subfolders to Search Path
Add c:/matlab/myfiles and its subfolders to the search path.
Call genpath inside of addpath to add all subfolders of c:/matlab/myfiles to the search path.
addpath(genpath('c:/matlab/myfiles'))
additional options can be found in the addpath documentation.
also use "pwd" to get the current path. in the end you probably are looking for:
addpath(genpath(pwd))

6 Comments

Yes i found this answer as well. However, after I upload my file onto the server, I no longer know the part 'c:/matlab/'. It will be changed.
The "however" is probably no problem since most m-files are run with the current folder being the folder that the m-file lives in. It doesn't matter if it's c:/matlab or anything else. As long as you run the file from the same folder where the m-file lives, you will be okay.
However if is is run from a different folder, but are able to because the folder your m-file lives in is on the path, then you won't get the subfolders of your m-file's folder, but of the current folder instead. In that case, see my answer.
exactly the point of the final line of my answer. so if you use pwd you know where you'll be starting from when you start the script.
example: in my matlab folder under c:\users\joseph.cheng\documents\matlab folder i have folder called matlabAnswers for a place to store scripts for this site. Lets say i have an image called penny.jpg in the matlabanswers folder. if i go:
currentdir = pwd;
imread(fullfile(currentdir,'matlabAnswers','penny.jpg'))
Now by doing so i can now copy both matlabAnswers folder + whatever script the above code is in any where. so if i keep the same folder structure and move it onto my thumbdrive (h:\) then currentdir would change and reflect wherever i put it on my thumbdrive.
Now before you go and say i'm not trying to read in images this is just an example on the use of pwd to build off of not knowing where your current path is.
so if you follow the example your commented 'user/example/matlab/current_use' will now be subsituted by pwd.
Joseph, pwd is the current folder, which is not necessarily the folder of the m-file you're running. So while the pwd may work, and probably will 99% of the time, it's not guaranteed to work.
Let's say you have test.m in d:\test, and then you also have two subfolders d:\test\t1 and d:\test\t2. Let's say that d:\test is on the search path, but is not the current folder. Now let's say you have another folder d:\mfiles, with two subfolders d:\mfiles\m1 and d:\mfiles\m2.
Now, let's say d:\mfiles is the current folder (it may or may not be on the path - it doesn't matter because it's the current folder). Let's say you type test on the command line. Well test.m is not in the current folder d:\mfiles, but it is in d:\test which is on the path, so test.m will get run because it's on the search path.
Now, to illustrate which folders genpath() generates, let's say that test.m has these lines in it:
folder = fileparts(which(mfilename));
p1 = genpath(folder) % Shows subfolders of d:\test
p2 = genpath(pwd) % Shows subfolders of d:\mfiles
Now the two genpaths will show different sets of folders.
p1 calls genpath that uses the actual folder where test.m lives, which is d:\test. So p1 will include d:\test, d:\test\t1, and d:\test\t2. This is what he wants.
p2 will include subfolders of the current folder, which recall was d:\mfiles. So p2 will include d:\mfiles, d:\mfiles\m1, and d:\mfiles\m2. This is not what he wants.
So that's why I gave my answer using "which" rather than use pwd, though I admit at first I did think of pwd until I wondered if that was robust.
oh but it really all depends on the implementation. I was under the impression the person was moving the whole working directory on to the network and knew where it was located but had hardcoded the subfolder paths into the script thus pwd would suffice. What i didn't think of was that the person may call the script file from another directory without navigating to the directory first. which yes i'd agree this wouldn't work.
While addpath works, is it really wise to use it for data files instead of implementing something that uses dir to get folder/file paths?
You're right. I never use addpath in an m-file except for my startup.m file where I need to setup some folders on the search path. It's much better to just specify the full path name (folder + base filename + extension) than to rely on cd or whatever may happen to be in the search path. The full path could even be relative paths, like '.\t1'. You could even do it in this case instead of using genpath. There is a related FAQ entry: http://matlab.wikia.com/wiki/FAQ#Where_did_my_file_go.3F_The_risks_of_using_the_cd_function.

Sign in to comment.

Aasim
Aasim on 30 Aug 2017
Edited: Aasim on 30 Aug 2017
Its also useful if you add all the functions that are not strict part of your project but you want to use them. you can put them all in a folder, lets say 'extended functions' but you want them to be in separate folders for easy search in the future. you can add them all in this way.
it does solve problem for me and i can keep track of my functions easily. hugs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(isunix) %just to use the right symbol for the path
symb='/';
else
symb='\';
end
baseFolder='path of the base folder /extendedFuntions';
addpath(baseFolder);
addingfolders=dir(baseFolder);
for folderid=3:size(addingfolders,1)
addpath([baseFolder,symb,addingfolders(folderid).name]);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

2 Comments

I know this is later to the party, but I just came to point out that the function
filesep
returns the correct file separator for the current platform to hopefully save people time and lines in the future!
Yes, but you really never need it if you use fullfile() since fullfile() builds the path using the proper file separator so there is no need for you to ever worry about it.
Anyway, all of that Aasim's code could be done much more simply in a single line of code using genpath():
addpath(genpath(baseFolder));

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!