How can I add all subfolders to my MATLAB path?

2 128 views (last 30 days)
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

Image Analyst
Image Analyst on 7 Oct 2015
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
Image Analyst
Image Analyst on 12 Nov 2019
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().
Orion Smedley
Orion Smedley on 27 Jun 2022
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)

Joseph Cheng
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
Joseph Cheng
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
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.

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

Sign in to comment.

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!