user5461722
user5461722

Reputation: 55

Batch script for new instances of MATLAB

I am trying to write a batch script which can call and run a MATLAB script in the following manner:

matlab -r plotFunction(a,b); quit %here, a=1:10 and b=1:10 
matlab -r plotFunction(a,b); quit %in 2nd instance a=11:20, b=11:20
matlab -r plotFunction(a,b); quit %in 3rd instance a=21:30, b=21:30 
and so on.

That is, each time a new instance of MATLAB opens up, calls the function plotFunction which performs plotting a 100 times and then the program (MATLAB) quits. Subsequent to this, another instance of the program opens, performs plotting a 100 times again (corresponding to a=11:20 and b=11:20) and quits again. And so forth. How to put this in a loop?

Upvotes: 0

Views: 131

Answers (2)

user664303
user664303

Reputation: 2063

The batch_job toolbox does this for you.

Upvotes: 1

Nigel Davies
Nigel Davies

Reputation: 1690

You can pass variables defined in the Windows command prompt to MATLAB line this:

set AMIN='1'
set BMIN='1'
set AMAX='10'
set BMAX='10'
matlab -r "disp(str2double(%AMIN%):str2double(%AMAX%)),disp(str2double(%BMIN%):str2double(%BMAX%)); input('press a key to quit'); quit"

Edit:

This can be improved like this,

set AMIN=1
set BMIN=1
set AMAX=10
set BMAX=10
set MATPROG=^
arange=(%AMIN%:%AMAX%),^
brange=(%BMIN%:%BMAX%),^
[x,y]=meshgrid(arange,brange),^
aplusb=x+y,^
plot3(x,y,aplusb),^
input('press a key to quit'),^
quit
matlab -r "%MATPROG%"    

Note that ^ is the batch file line continuation character.

This change will make it easier to convert to loops in the batch file, although I don't understand why you don't create the loops in a MATLAB function and call that instead to keep the batch files as simple as possible.

Upvotes: 0

Related Questions