1

Is there a way to run multiple .m files in different instances of MATLAB concurrently through a batch file? The task that I want to accomplish is like below:

  1. Open x instances of MATLAB;

  2. Let different instances run different .m files simultaneously (so that my CPU power could be completely utilized);

  3. When all instances finish, exit all MATLAB.

Could a single batch file accomplish this process? The reason I want to use a single batch file to do so is that I want to call this batch file in my MATLAB script. Essentially, I want to do parallel computation. (Since some of my scripts have to be run with MATLAB 2007, which doesn't have the parallel computing toolbox, I have to find a way around.)

Please explain the syntax of your code since I know little about the command prompt.

Currently, I only know how to do the task in sequence, just like the code shown below.

cd "C:\My_MATLAB_folder_path"

matlab r- "mfile01;exit"

matlab r- "mfile02;exit"

...

matlab r- "mfilexx;exit"

If a command prompt could not achieve this process, what alternative methods could I use? One important thing is that whatever method may be used, it must be able to be called in a MATLAB script.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Siyu Jiang
  • 11
  • 6
  • 1
    When CMD executes a command or program from a batch file it waits for the launched program to exit, then executes the next command. To overcome this, you can prefix each invocation of MATLAB with `start` command: `start "" matlab r- "mfilexx;exit"`. This is just from the batch scripting viewpoint , but I neither recommend nor reject the idea of invoking too many simultaneous instances of MATLAB. But there must be a limit that you should consider. Also putting a slight delay between the MATLAB invocations might be good idea `timeout `. – sst Aug 07 '18 at 06:07
  • 1
    @sst You can achieve this natively in MATLAB with the parallel computing toolbox. When doing that, the default number of worker instances is equal to the number of cores on the machine, which might be useful as a guideline for how many instances to use with this method. – Wolfie Aug 07 '18 at 08:08
  • @Wolfie, Yea I was thinking the same. It makes sense. Thanks for the info. – sst Aug 07 '18 at 12:31
  • @sst Thanks, that works. An additional request: Could you put your comment in answer so that I can choose yours as the best answer then close the question? – Siyu Jiang Aug 07 '18 at 18:32

1 Answers1

0

When CMD executes a command or program from a batch file it waits for the launched program to exit, then executes the next command. To overcome this, you can prefix each invocation of MATLAB with start command: start "" matlab r- "mfilexx;exit"

Also making a slight delay between MATLAB invocations might be a good idea to prevent putting hard disk under excessive stress.

So the task could be done with something like this:

@echo off
setlocal

REM Delay is in seconds after /t switch
set "delay=timeout /t 1 /nobreak >nul"
REM Or this for Windows XP: (Delay is in milliseconds after -w switch)
set "delay=ping -n 1 -w 1000 127.255.255.255 >nul"

cd /d "C:\My_MATLAB_folder_path"

start "" matlab -r "this.m;exit"
%delay%

start "" matlab -r "that.m;exit"
%delay%
...

Or Alternatively it can simplified by witting the batch script this way:

cd /d "C:\My_MATLAB_folder_path"
for %%A in (
    "this.m"
    "that.m"
    "other.m"
    "add each file in a new line or on the same line separated with space.m" "one another.m"
) do (
    start "" matlab -r "%%~A;exit"
    %delay%
)
sst
  • 1,443
  • 1
  • 12
  • 15
  • First of all, thanks for your answer. I have an additional question: Is it possible to "hold" the batch file while any of opened MATLAB is running? In my MATLAB script, the outputs of these .m files are used as inputs in the following script. Hence, before MATLAB could move on, all .m files should be finished. I tried to add /wait for the .m file opened at the last, hoping it will finish at the last as well. However, not all time the last-opened one is the last-finished one, which cause errors in my MATLAB script because non-sufficient inputs. How could I fix this? – Siyu Jiang Aug 10 '18 at 00:22
  • @SiyuJiang, I'm not sure if I understood you correctly. I assume you want to concurrently execute one set of MATLAB scripts and wait for all them to finish and then execute another set of MATLAB scripts. If that is the case then take a look at this [Answer](https://stackoverflow.com/a/11715437/4561332). However you can't use the code from that answer directly for your own purpose, but the used technic is what you can incorporate in your own code. Either way if you have any new question you should ask it by opening up another question. – sst Aug 10 '18 at 01:31
  • Sorry I tried to understand the Answer you gave me but failed. It's too advanced for a beginner like me. However, I found a much simpler solution at [link](https://stackoverflow.com/a/33586872/10189598) . But thank you anyway. – Siyu Jiang Aug 13 '18 at 19:03
  • @SiyuJiang, Glad you have found a working solution. Yes the solution from the link you have posted is more simpler and is efficient as well. But the underlying concept is the same. – sst Aug 13 '18 at 23:33