Add folder and subfolder to project path
显示 更早的评论
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 个评论
Chuguang Pan
2025-11-12
dpb
2025-11-12
The current documentation for both addpath and addPath (that's bloody confusing and rife for error!) says both can use cellstr or string arrays of multiple folders.
Double-check about the specific R2024b doc, though; perhaps that is a new addition since.
In prior versions, addpath accepted a list of comma-separated folders in the argument as
addpath(folder1, folder2, ...)
I'd suggest to try that syntax.
If the doc for your specific release does, indeed, match, then it would appear to be a bug. That doesn't help your immediate problem, of course, but would be time to contact official Mathworks support.
Niccolò
2025-11-12
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
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.
采纳的回答
更多回答(1 个)
v=ver; v(1).Release
help 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')
%addPath
The project-related stuff isn't available on this platform, even...
l=readlines(f) % let's explore the implementation
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Search Path 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!