In the Windows OS environment, a batch file that runs a Matlab script will continue on immediately after Matlab is called. This have to do with Matlab starting a new processes and then terminates the original called process immediately. This cause the batch file to interpret Matlab as being done and so the batch file carries on. Meaning, in the following example, the batch file will try to copy the file GenerateStockFile.txt before the GenerateStockFile.m finish generate the file.
matlab -r "run GenerateStockFile;exit;" -nosplash -nodesktopThe solution based on what I find via http://serverfault.com/questions/245393/how-do-you-wait-for-an-exe-to-complete-in-batch-file is to insert a loop to check if Matlab finishes before moving on. I.e.,
copy /Y GenerateStockFile.txt c:\Results
matlab -r "run GenerateStockFile;exit;" -nosplash -nodesktop"tasklist /fi "imagename eq MATLAB.exe" generates the following output if Matlab is not currently being run, and the code following the pipe (|) checks to see if the output contains the ":" character.
:loop
sleep 10
tasklist /fi "imagename eq MATLAB.exe" | find ":" > nul
if errorlevel 1 goto loop
sleep 20
copy /Y GenerateStockFile.txt c:\Results
INFO: No tasks are running which match the specified criteria.The SLEEP lines are there just so the batch files doesn't constantly run TASKLIST.EXE to check. Also, if you use UnxUtils, which replaces the Windows FIND command with the UnxUtils' FIND command, the using the GREP command instead works just as well. I.e.,
tasklist /fi "imagename eq MATLAB.exe" | grep ":" > nulThe page I referenced also suggest using "|more" and "start /wait". However, those methods did not work for my particular situation.