How can I add all subfolders to my MATLAB path?
Show older comments
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
More Answers (2)
Joseph Cheng
on 7 Oct 2015
Edited: Joseph Cheng
on 7 Oct 2015
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
dragoon
on 7 Oct 2015
Image Analyst
on 7 Oct 2015
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.
Joseph Cheng
on 7 Oct 2015
Edited: Joseph Cheng
on 7 Oct 2015
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.
Image Analyst
on 7 Oct 2015
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.
Joseph Cheng
on 7 Oct 2015
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?
Image Analyst
on 7 Oct 2015
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.
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
Brian
on 26 Feb 2020
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!
Image Analyst
on 26 Feb 2020
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));
Categories
Find more on Search Path in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!