Error with reading file within a parfor loop in a compiled application

5 ビュー (過去 30 日間)
HpW
HpW 2023 年 9 月 29 日
コメント済み: Walter Roberson 2023 年 9 月 29 日
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
Bruno Luong 2023 年 9 月 29 日
A workaround is perhaps calling getcurrentdir outside the parfor (then pass it to the worker)

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 9 月 29 日
You should be using ctfroot
  4 件のコメント
HpW
HpW 2023 年 9 月 29 日
Thanks. Yes, that is my issue. I have to store the csv file with the exe in the same directory that is not the temp directory that ctfroot points to.
Walter Roberson
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 ExchangeParallel 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