running matlab function multiple times from windows command line

8 views (last 30 days)
My matlab code is heavy in terms of memory and cpu cycles and takes days to finish. I need to re-run the code for 10 times. I am doing that using a matlab script, but the machine crashes due to out of memory error. Matlab help documentation recommends for such type of codes to save the work space and restart matlab frequently to avoid memory fragmentation. Therefore, I created the following simple windows script to automate restarting matlab:
@echo off
for /l %%x in (1, 1, 10) do (
matlab -nodisplay -nojvm -nosplash -nodesktop -r "Matlab_function(,);exit"
timeout 30 >nul
It seems to me that still multiple instances of matlab are running together. My question is how to ensure that the next call to the matlab function do not happen until the current call is completely finished and the matlab command window is closed? any idea?

Answers (3)

Prasanth Sunkara
Prasanth Sunkara on 22 Jan 2018
Hello Mustafa, What I understand is you basically want to restart MATLAB every 30 min through a windows shell script. I would suggest you to modify the code a little as shown below: @Line 4: TIMEOUT /T 1800 /NOBREAK
  1 Comment
Mustafa Al-tekreeti
Mustafa Al-tekreeti on 22 Jan 2018
Hello Prasanth Thanks for your response, the problem is my matlab function is heavy in terms of cpu cycles and memory, because I have randomness in the code, I need to run the matlab function many times and evaluate the mean of whatever I want to get, one run of the code can take 2 to 3 days, because running matlab for days can cause memory fragmentation that may lead to out of memory error, it is recommended to restart matlab frequently to avoid this possibility, so this is the purpose behind this shell script, I need to run the code, exit matlab, insert a delay of 30 seconds, and then rerun the code again and so on. I want to ensure that only one matlab function is running at one time, but unfortunately, I am still seeing multiple matlab command windows are booting simultaneously, which causes the system to crash

Sign in to comment.

Benjamin Marechal
Benjamin Marechal on 29 Mar 2018
Just add '-wait' option to the command line: matlab -wait -nodisplay -nojvm -nosplash -nodesktop -r "Matlab_function(,);exit"
Without this option, your batch script will continue even if the matlab function hasn't finished.
You can check it by writing the command : matlab -help which gives :
-wait - MATLAB is started by a separate starter program
which normally launches MATLAB and then immediately
quits. Using the -wait option tells the starter
program not to quit until MATLAB has terminated.
This option is useful when you need to process the
the results from MATLAB in a script. The call to
MATLAB with this option will block the script from
continuing until the results are generated.
Using "-wait", you don't really need the timeout.

Steven Lord
Steven Lord on 29 Mar 2018
When you say " I am doing that using a matlab script, but the machine crashes due to out of memory error. " do you mean MATLAB crashes, the machine on which you're running MATLAB crashes, or MATLAB throws an error indicating it is out of memory? In the first two cases, that obviously shouldn't happen and you should send any crash log you received from MATLAB to Technical Support for further investigation.
In the last case, how large a block of data are you trying to process? Are you using a release that supports datastore and tall, whose main purpose is to work with data so large it can't fit in memory as one piece? If you are using a release that includes tall and you want to try to see if it avoids the out of memory error, check the documentation included in your installation to make sure the functions you want to use are supported for tall. [The list of functions that support tall increases every release, so if you're not using the most recent release the list on the page to which I linked earlier will include functionality that won't work on your older release.]


Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by