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

5 views (last 30 days)
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!

Answers (1)

Walter Roberson
Walter Roberson on 29 Sep 2023
You should be using ctfroot
  4 Comments
HpW
HpW on 29 Sep 2023
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 on 29 Sep 2023
You would potentially need to do that if you are updating the csv without updating the executable.

Sign in to comment.

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!