0

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?

4
  • More importantly, which operating system are you using? Less importantly, which version of MATLAB? Commented Dec 22, 2015 at 8:30
  • 1
    Why don't you write a MATLAB script to the all the looping and plotting? It might help if you explain why you need to do this in a batch script. Commented Dec 22, 2015 at 8:33
  • OS is windows: One batch running on XP, one on Win 8, one on Win 10, one on Server 2012 and one on Win 7. MATLAB version is 2015b. Commented Dec 22, 2015 at 8:42
  • Take a look at the for /L command (type for /? in command prompt to see details); and also set /A for the arithmetic operations... Commented Dec 22, 2015 at 8:58

2 Answers 2

1

The batch_job toolbox does this for you.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Suppose the function is a simple addition: 'function aPlusb=sumFunction(a,b) aPlusb=a+b;' The first instance has to call the function (sumFunction) a 100 times corresponding to a=1:10 and b=1:10 - how to incorporate this?
Do you want the loop in the batch file. If so, why? It is much easier in MATLAB. See for example meshgrid. Otherwise there's lot of information on looping in batch files if you search, like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.