Error with reading file within a parfor loop in a compiled application
2 次查看(过去 30 天)
显示 更早的评论
Hello
I have an application that has a parfor loop in it. Part of the loop reads a .csv file and compares some values. When I run the program via MATLAB it runs perfectly fine. I have also compiled the program, but when I run the compiled program I get an error related to the file:
Unable to find or open 'C:\Program Files\MATLAB\R2022a\bin\win64\file.csv'. Check the path and filename or file permissions.
Possibly interesting is that the directory in the error is NOT where the file is..... the file is in the directory where the exe is being run from which I have obtained using the following code:
F = readcell(fullfile(getcurrentdir(),'file.csv'))
using this function to find the directory being used to execute the program:
function currentDir = getcurrentdir()
if isdeployed && ispc
[status, result] = system('set path');
currentDir = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
It seems like getcurrentdir() does NOT return the directory from where the compiled program is running when called inside the parfor loop - instead it returns this other directory 'C:\Program Files\MATLAB\R2022a\bin\win64\'
I'm suspecting this has to do with calling getcurrentdir() from within the parfor loop since it does NOT happen when I run like a normal for loop by setting the number of workers to 0
Why does this only happen when its compiled? It runs perfectly when running via MATLAB source code.
Is there a way to deal with this issue? Do I have to pass the data from file.csv into the function that runs the parfor loop so that It does not have to read the file in the actual loop?
Any help would be appreciated.
Thanks!
1 个评论
Bruno Luong
2023-9-29
A workaround is perhaps calling getcurrentdir outside the parfor (then pass it to the worker)
回答(1 个)
Walter Roberson
2023-9-29
4 个评论
Walter Roberson
2023-9-29
You would potentially need to do that if you are updating the csv without updating the executable.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!