Add folder and subfolder to project path

66 visualizaciones (últimos 30 días)
Niccolò
Niccolò el 12 de Nov. de 2025 a las 13:59
Comentada: dpb el 13 de Nov. de 2025 a las 14:23
Hi everyone,
I’m trying to use the addPath function to add multiple folders to a MATLAB project’s path. According to the documentation, the folders input argument should be:
"Path of the folders to add to the project path, specified as string array or as a cell array of character vectors. The folders must be within the root folder."
Here’s a minimal example of what I tried:
tmp = ["Hello", "Hello\World"];
addPath(proj, tmp);
but I got this error:
Error using matlab.internal.project.japi.Project/addPath
Expected file to be of size 1x1, but it is of size 2x1.
Error in matlab.project.Project/addPath
Even though tmp is a string array, MATLAB seems to expect only a single path.
For context:
  • The folders already exist within the project
  • I’ve added them (and their subfolders/files) using addFolderIncludingChildFiles, but that function doesn’t add the folders to the project path
  • I’m using MATLAB R2024b
  • I’m trying to use addPath with multiple folders at once because adding each folder path in a for loop is very time-consuming when dealing with many subdirectories.
Has anyone managed to add multiple folders at once using addPath, or is it necessary to call it one folder at a time?
Any suggestions or workarounds would be greatly appreciated!
Thanks in advance!
  4 comentarios
Niccolò
Niccolò el 12 de Nov. de 2025 a las 15:45
Thank you all for your replies!
I tried using a comma-separated list of folders, but that gives me a "Too many input arguments" error.
Looking at the documentation for R2024b, it does seem different from what’s actually implemented:
It looks like in older MATLAB versions, the addPath function could only accept a single folder at a time.
By the way, do you know why this function takes so long to execute? On my machine, it takes around 0.5 (even >1) seconds per call, which makes adding many folders quite slow.
dpb
dpb el 12 de Nov. de 2025 a las 15:51
Editada: dpb el 12 de Nov. de 2025 a las 16:59
Submit this to Mathworks as an official support request/bug at <Product Support Page>.
I don't have the new releases installed so don't know anything about the new project structure, sorry.
A cause for some of the slowness is internally there's a function catdirs that includes
n=nargin-1;
narginchk(2,inf)
cdirs = '';
for i=1:n
next = varargin{i};
% Remove leading and trailing whitespace
trimmedNext = strtrim(next);
if ~isempty(trimmedNext)
...
cdirs = [cdirs trimmedNext pathsep]; %#ok<AGROW>
end
end
which grows the cdirs list dynamically. Normally, this wouldn't be terribly time consuming, but it is building a long char() vector string that is the culmination of everything altogether as one long string which can get to be a quite sizable array if there are a lot of folders or the path depth is long.
Well, just for grins, let's see what it is here--
p=path;
whos p
Name Size Bytes Class Attributes p 1x111926 223852 char
That's pretty long already although not a huge amount of memory by today's standards, of course. But, it's surely not inconsequential.
This could be slow if the present path is already long although your observation does seem unusually so. Is a lot of other memory also allocated at the time this is happening? It almost sounds like disk swapping to be so long as if had a memory problem.

Iniciar sesión para comentar.

Respuesta aceptada

Steven Lord
Steven Lord el 13 de Nov. de 2025 a las 6:36
From the documentation page's Version History section: "Starting in R2025a, the addPath function supports adding multiple folders to the project path at the same time." You're using release R2024b, which predates the introduction of that functionality.
Note that this addPath function is a method of matlab.project.Project objects, as opposed to the addpath function which is not a method of a class but is a regular function.
  1 comentario
dpb
dpb el 13 de Nov. de 2025 a las 14:23
Yeah, discovered that below....the two different only by capitalization is a potential confusion, certainly; that's not a typical MATLAB practice so will catch folks off guard I expect with one when expecting the other.
More significantly, any ideas about why @Niccolò is seeing such drastically long execution times?

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 12 de Nov. de 2025 a las 15:44
Movida: dpb el 12 de Nov. de 2025 a las 15:45
v=ver; v(1).Release
ans = '(R2025b)'
help addPath
--- addPath not found. Showing help for addpath instead. --- addpath - Add folders to search path This MATLAB function adds the specified folders to the top of the search path for the current MATLAB session. Syntax addpath(folderName1,...,folderNameN) addpath(folderName1,...,folderNameN,position) addpath(___,'-frozen') oldpath = addpath(___) Input Arguments folderName1,...,folderNameN - Folder names to add to search path character vectors | string scalars position - Position on search path '-begin' (default) | '-end' Output Arguments oldpath - Path prior to addition of folders character vector Examples openExample('matlab/AddFoldertoTopofSearchPathExample') openExample('matlab/AddFoldertoEndofSearchPathExample') openExample('matlab/AddFolderandItsSubfolderstoSearchPathExample') openExample('matlab/AddFoldertoSearchPathandDisableFolderChangeNotificationExample') See also genpath, path, pathsep, rmpath, savepath Introduced in MATLAB before R2006a Documentation for addpath doc addpath
There seems to be a mismatch here; it looks like the doc says can use arrays but the implementation must still expect the list.
f=which('addPath','all')
f = '/MATLAB/toolbox/matlab/general/addpath.m'
%addPath
The project-related stuff isn't available on this platform, even...
l=readlines(f) % let's explore the implementation
l = 99×1 string array
"function oldpath = addpath(varargin)" "%ADDPATH Add folder to search path." "% ADDPATH FOLDERNAME prepends the specified folder to the current" "% matlabpath. Surround FOLDERNAME in quotes if the name contains a" "% space. If FOLDERNAME is a set of multiple folders separated by path" "% separators, then each of the specified folders will be added." "%" "% ADDPATH FOLDERNAME1 FOLDERNAME2 FOLDERNAME3 ... prepends all the" "% specified folders to the path." "%" "% ADDPATH ... -END appends the specified folders." "% ADDPATH ... -BEGIN prepends the specified folders." "% ADDPATH ... -FROZEN disables folder change detection for folders" "% being added." "%" "% Use the functional form of ADDPATH, such as" "% ADDPATH('folder1','folder2',...), when the folder specification is" "% a variable or string." "%" "% P = ADDPATH(...) returns the path prior to adding the specified paths." "%" "% Examples" "% addpath c:\matlab\work" "% addpath /home/user/matlab" "% addpath /home/user/matlab:/home/user/matlab/test:" "% addpath /home/user/matlab /home/user/matlab/test" "%" "% See also RMPATH, PATHTOOL, PATH, SAVEPATH, USERPATH, GENPATH, REHASH." "" "% Copyright 1984-2024 The MathWorks, Inc."
Well, here it's still the many versus array.
Looks like time for a bug report or at least documentation correction if you see same thing on local system.
As noted above, try using the list of folders form; it wilt likely work.

Categorías

Más información sobre Search Path en Help Center y File Exchange.

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by