Thursday, June 25, 2015

Batch script to automate simulations in MATLAB

Many of us use MATLAB to perform simulations. Often such simulations requires multiple runs of the same program. Suppose we are simulating an ODE -based model for a cell signaling pathway. We have created a main function (say Main.m) that execute the simulation while calling and using other functions. We want to simulate the same model with large number of different values of a particular parameter. One way of doing it, is to modify the code itself to run multiple simulations with different sets of parameter values. But, I would prefer not to tamper with the code. The other option is to change the parameter value and run the simulations one after another. That's bit cumbersome and unprofessional. Can't we automate the process?

Yes, we can. Don't hard code the parameters in the main code. Let it read the parameter values from a separate file. That can be a text or m-file. Make multiple folders having all the codes and parameter file. Change the values in the parameter files in these folders. Now use a script to run the simulation from each of these folder, one after another. Note that each of the sub-folder should have all the codes to run the simulations independently.

Even, you can use this strategy to, sequentially, run completely different simulations from different sub-folders. The only requirement is that you have to use same name (Main.m) for all the main functions. 

The batch script written bellow does exactly that. My PhD student Vimalathithan wrote the script. If you find it useful, do give him the credit.

This may not be the only way or best way to automate multiple runs. Please do write back, with your take on this issue.

batch_script.bat
:: -The script automates sequential repeated run of same Matlab code. 
:: -The user should enter the location of the parent folder which contains the Sub models
:: -The user should enter the location of matlab.exe file
:: -Every event is recorded in a LogFile.txt in the parent directory
:: -The scripts does the following job 
::  1.Creates a text file holding the name of the sub folders present in the parent directory
::  2.Executes each sub folder by invoking the main function (Main.m)
::  3. If the function is not found, the error message is recorded and the loop continues with the next sub folder.
::  4.If matlab throws any error, the error message is recorded and the loop continues with the next sub folder
::-------------------------------------------------------------------------------------------------------------------------
::Enter the location of the Model 
set ModelDir=D:\Dropbox\modeling_tool\MATLAB_vimal\ODEmodeling\ODE1

::Enter the location of the Matlab.exe file
set MatlabDir=C:\matlab\bin\matlab.exe
::-------------------------------------------------------------------------------------------------------------------------
::Do not make any changes in the code below
::-------------------------------------------------------------------------------------------------------------------------
cd %ModelDir%
Echo Start time is %date%_%time%>LogFile.txt
::Lists the folders in the current directory and writes them in a text file
dir /A:D /O:N /B>List.txt
setlocal enableextensions enabledelayedexpansion
Echo off
::Counts the number of lines in the text file
for /F %%A in ('type "List.txt"^|find "" /V /C') do (
 set /A count=%%A
)
set /A num=1
::Executes the matlab code present in each sub folder
for /F "tokens=*" %%A in  (List.txt) do (
 Echo.>>%ModelDir%\LogFile.txt
 Echo Executing !num!-%%A of %count%
 cd %ModelDir%\%%A
 IF EXIST ErrorFile.txt (
  del ErrorFile.txt
 )
 IF EXIST %ModelDir%\%%A\Main.m (
  "%MatlabDir%" -nodisplay -nosplash -nodesktop -wait -r "run('Main.m');exit;"
  IF EXIST %ModelDir%\%%A\ErrorFile.txt (
   Echo %%A is not executed>>%ModelDir%\LogFile.txt
   Echo WARNING. Matlab has logged an error file in the location %ModelDir%\%%A>>%ModelDir%\LogFile.txt
  )
  IF NOT EXIST %ModelDir%\%%A\ErrorFile.txt (
  Echo %%A is executed successfully>>%ModelDir%\LogFile.txt
  )
 ) 
 IF NOT EXIST %ModelDir%\%%A\Main.m (
  Echo %%A is not executed>>%ModelDir%\LogFile.txt
  Echo WARNING. Main function is not found in the location %ModelDir%\%%A>>%ModelDir%\LogFile.txt
 )
 set /A num+=1
 cd %ModelDir%
)
endlocal
Echo.>>%ModelDir%\LogFile.txt
Echo End time is %date%_%time%>>LogFile.txt
::Deletes the List.txt file
del List.txt
Echo on