Why am I getting an error that my file cannot be found when running my Simulink model with "parsim"?

20 views (last 30 days)
In my Simulink model, one of my model callbacks loads a file from my current directory using the following command:
load('./somefile.txt')
When I run this model using the “sim” command, it works as expected and the file is loaded properly.
However, when I run this model using the “parsim” command, I receive an error message saying that my file cannot be found in the current directory. After some testing, I have found that I can resolve this error by specifying the full path for the file rather than a relative path. That being said, my workflow requires that the input to the “load” function must be a relative path.
When using “parsim”, how can I load my file while still using a relative path?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 8 Nov 2023
Edited: MathWorks Support Team on 8 Nov 2023
This error message stems from the fact that, when running a model with “parsim”, MATLAB changes the current working directory to a temporary directory.
This behavior can be tested by running the “pwd” command before and during the execution of the simulation. When using “sim”, the output will be the same for both function calls. However, when “parsim” is used, the outputs will differ, with the latter call pointing to a directory in your “Temp” folder.
To resolve this error message, try one of the following workarounds:
1) Instead of loading the file in a model callback, use the "SetupFcn" argument of "parsim”. This argument acts similarly to a "PreLoadFcn" model callback, and executes whenever the Simulink model is loaded onto each worker. For more information on the "SetupFcn" property, please see the following documentation:
2) If your workflow requires loading the file during simulation, remove the “./” from the input of the “load” function. For example, use the following line of code:
load('somefile.txt')
3) If your workflow requires loading the file during simulation and you must include the “./” in the input syntax, copy the necessary file into the temporary directory. The following code snippet, run by each worker during “parsim”, would achieve this:
originalpath = which('somefile.txt')
desiredpath = fullfile(pwd,'somefile.txt')
copyfile(originalpath,pwd)
load('./somefile.txt')

More Answers (0)

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!