The "parfeval" command is not working on the addPath function

8 次查看(过去 30 天)
I have a cell array that contains the folder paths that I want to add to the project path. Initially, I used the clasic for loop; however, as the cell array contains more than 2K folder paths, this clasic for loop takes more than 2 hours to complete the task. To overcome this issue, I have used the "parfeval" command in the for loop, but I am getting the error mentioned below.
Error Details:
Caused by:
Error using matlab.project.Project/addPath
Dot indexing is not supported for variables of this type.
How can i resolve this issue?
Below is my code:
%get current project
proj_modelBasedDesignCodeGen = currentProject;
% List of folder paths
FolderPaths = {'\components\ComponentA', '\components\ComponentB', '\components\ComponentC', '\components\ComponentD'};
% Preallocate cell array to store output
futures = cell(1, numel(FolderPaths));
% Loop through each folder path and submit a task to the parallel pool
for i = 1:numel(FolderPaths)
inputFolder = FolderPaths{i};
% Use parfeval to asynchronously evaluate function addPath
futures{i}= parfeval(@addPath, 1,proj_modelBasedDesignCodeGen, inputFolder); % 1 is the number of output arguments
end

回答(1 个)

Zinea
Zinea 2024-6-5
The function "parfeval" is causing the error, as it attempts to operate on the "proj_modelBasedDesignCodeGen" object. This issue arises from MATLAB's handling of parallel processing in conjunction with its project objects. When using “parfeval”, the function and its inputs are serialized and sent to the workers. However, MATLAB project objects (“matlab.project.Project”) are not designed to be serialized or passed to workers in a parallel poop, hence the error occured.
A workaround for this issue is to not directly manipulate the project path within the parallel workers. Instead, you can collect all the paths you want to add and then add them to the project outside of the parallel loop. If the goal is to speed up the addition of many folders to the project path, consider using a single call to add these paths instead of adding them one by one.
Here's the approach:
% Assuming FolderPaths are relative to the project's root folder
rootPath = proj_modelBasedDesignCodeGen.RootFolder;
absoluteFolderPaths = fullfile(rootPath, FolderPaths);
% Add paths to the project (one by one, as required by the API)
for i = 1:numel(absoluteFolderPaths)
addPath(proj_modelBasedDesignCodeGen, absoluteFolderPaths{i});
end
Here are the documentation links to “addPath” and parfeval” for your reference:
Hope it helps!

类别

Help CenterFile Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by