running matlab function multiple times from windows command line

5 ビュー (過去 30 日間)
Mustafa Al-tekreeti
Mustafa Al-tekreeti 2018 年 1 月 11 日
回答済み: Steven Lord 2018 年 3 月 29 日
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
)
pause
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?

回答 (3 件)

Prasanth Sunkara
Prasanth Sunkara 2018 年 1 月 22 日
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
-Prasanth
  1 件のコメント
Mustafa Al-tekreeti
Mustafa Al-tekreeti 2018 年 1 月 22 日
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

サインインしてコメントする。


Benjamin Marechal
Benjamin Marechal 2018 年 3 月 29 日
Hello!
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 2018 年 3 月 29 日
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.]

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by