I have a script that opens a MATLAB command window and inputs command in it: # Command to start MATLAB and run the script matlab_command = [ matlab_executable, '-nodesktop', '-nosplash', '-r', matlab_command_string ] # Start MATLAB process process = subprocess.Popen(matlab_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) However, I'd like to be able to set a flag when the MATLAB window closes, which it does right after finishing the MATLAB tasks. I tried to achieve that with a process.wait() but I still get the return code 0 after all the Python tasks are executed, not after the MATLAB subprocess is done. I thought with the process.wait() the code would actually track the subprocess state, but no, since it opens a MATLAB window, the Python subprocess only tracks the initial process that spawns the window, not the full MATLAB session. Continue reading...