How to prevent Matlab from immediately running after using system function to call an .sh script?

11 views (last 30 days)
I'm running a Matlab script on windows 10, which in turns calls a shell script file in order to run some simulations on Ubuntu using bash. By definition the Matlab "system" function waits for the .sh file to finish before continuing the executation of the Matlab script. The problem arises when the .sh function calls a subprocess, because in that case Matlab doesn't wait for this subprocess to finish and continues the execution of the Matlab script. Does somebody knows how to force Matlab to wait for the total completion of the .sh script before continuing execution?

Answers (1)

Precise Simulation
Precise Simulation on 5 Mar 2020
Not perfect but for the FEATool Multiphysics toolbox a lot of exernal system calls are used, for example for grid and mesh generation and solvers such as OpenFOAM, and for this temporary "lock" files work quite well.
In this case the Matlab code would call the system call with the & postfix as "system( 'my_script.sh &' )" after which a while loop checks for the existance of a "lock" file and exits the loop when not found. The shell script would first write an empty file like "C:\temp\lock" and then delete it after the last script action. It is not perfect and locks up the Matlab thread, but can be made quite robust and handle error cases.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!