Use Matlab's External Interfaces support to get access to a lower level language's process control features.
.NET Version
Use the .NET System.Diagnostics.Process class. It'll let you run a process asynchronously, check for when it's exited, and collect the exit code. And you can optionally hide its window or leave it visible for debugging.
You can call .NET classes directly from M-code.
function launch_a_bat_file()
%//LAUNCH_A_BAT_FYLE Run a bat file with asynchronous process control
batFile = 'c:\temp\example.bat';
startInfo = System.Diagnostics.ProcessStartInfo('cmd.exe', sprintf('/c "%s"', batFile));
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; %// if you want it invisible
proc = System.Diagnostics.Process.Start(startInfo);
if isempty(proc)
error('Failed to launch process');
end
while true
if proc.HasExited
fprintf('\nProcess exited with status %d\n', proc.ExitCode);
break
end
fprintf('.');
pause(.1);
end
Java Version
The .NET version requires a Matlab new enough to have .NET support. Here's a Java-based version for older Matlabs, like OP turns out to have. Should also work on non-Windows systems with a bit of modification.
function launch_a_bat_file_with_java
%LAUNCH_A_BAT_FILE_WITH_JAVA Java-based version for old Matlab versions
batFile = 'c:\temp\example.bat';
cmd = sprintf('cmd.exe /c "%s"', batFile);
runtime = java.lang.Runtime.getRuntime();
proc = runtime.exec(cmd);
while true
try
exitCode = proc.exitValue();
fprintf('\nProcess exited with status %d\n', exitCode);
break;
catch
err = lasterror(); % old syntax for compatibility
if strfind(err.message, 'process has not exited')
fprintf('.');
pause(.1);
else
rethrow(err);
end
end
end
You may need to fiddle with the I/O in the Java version to avoid hanging the launched process; demarcmj notes that you need to read and flush the Process's input stream for stdout to avoid it filling up.