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

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 Comment

A workaround is perhaps calling getcurrentdir outside the parfor (then pass it to the worker)

Sign in to comment.

Answers (1)

You should be using ctfroot

4 Comments

Hi. Ctfroot does not seem to work for my purposes because the file.csv needs to be editable and is just included with the .exe file, its not part of the compiled program itself
I have a folder (which can be a user specified location) with compiledprog.exe and file.csv. I need the program to use the file in the same directory that the .exe is run from and which contains file.csv.
This only seems to be an issue when its called within a parfor loop. The compiled program gets the correct directory using the above code when it references some other external files outside of the parfor loop.
Thanks for your assistance!
If I understand correctly in theory the ctfroot can be different when the ctf is unpacked different times. And since in theory the unpacked ctf is considered "temporary", people might want to store files with the executable, I guess.
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.
You would potentially need to do that if you are updating the csv without updating the executable.

Sign in to comment.

Categories

Products

Release

R2022a

Asked:

HpW
on 29 Sep 2023

Commented:

on 29 Sep 2023

Community Treasure Hunt

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

Start Hunting!