Add folder and subfolder to project path
66 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
dpb
2025 年 11 月 12 日 15:51
編集済み: dpb
2025 年 11 月 12 日 16:59
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.
採用された回答
Steven Lord
2025 年 11 月 13 日 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 件のコメント
dpb
2025 年 11 月 13 日 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.
その他の回答 (1 件)
dpb
2025 年 11 月 12 日 15:44
移動済み: dpb
2025 年 11 月 12 日 15:45
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Search Path についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!