How can I add all subfolders to my MATLAB path?

1 967 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
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
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
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.

Community Treasure Hunt

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

Start Hunting!