MATLAB: How to force MATLAB 7.9 (R2009b) to wait for an EXE file to complete before continuing with the MATLAB script or function

executableMATLABprocessstatus

I call an executable (EXE) file in one of my MATLAB scripts. Now I want MATLAB to wait until the EXE file finishes its task as the code that follows the call requires outputs from the EXE.

Best Answer

You can use MATLAB to check if a particular process is running on the system and make MATLAB wait until this process is closed.
Here is a simple example that uses a DOS function SLEEP as the executable. Attached are two files testRun.m and isprocess.m. You will need the isprocess.m file to check the system processes for a particular exe.
testRun shows a sample:
% Do some tasks
% Call sleep.exe for 30 seconds
% (sleep makes dos "idle" for the given time, simulating if the exe were running)
!sleep 30 &
% MATLAB will continue execution once EXE is launched
disp('MATLAB continues after calling EXE')
% Check to see if the EXE process exists
flag = true;
while flag
disp('EXE still running');
flag = isprocess('sleep.exe'); % isprocess is the function attached. Place it on the MATLAB path.
pause(1) % check every 1 second
end
disp('EXE Done')
disp('continuing with MATLAB script')