Reputation: 1901
I managed to run Matlab on background with the help of the website below:
http://www.stat.osu.edu/computer-support/programming/background-jobs
I am performing the loop:
for ((i = 1; i <= 5; i++)); do
echo $i>i.txt;
matlab -nodesktop -nodisplay <script.m &> dummy.out &
done
in the script there is a part :
fid=fopen( 'a:\folder\i.txt');'];
iter=str2double(fgets(fid))
myfunction(iter,a,b,c)
the function line at myfunction.m is
myfunction(num,a,b,c)
this function saves a file with a name that is also changing according to the value of 'num'
meaning, the output will be: myfile1.mat for the 1st command, myfile2 for the 2nd command etc.
when I'm running the commands without the loop
echo 1>i.txt;
matlab -nodesktop -nodisplay <script.m &> dummy.out &
echo 2>i.txt;
matlab -nodesktop -nodisplay <script.m &> dummy.out &
etc...
there are no problems and the output is good
when I'm running the loop the only file i'm getting is myfile5.mat
I've changed the code so that the input will be myfunction(i1,a,b,c), myfunction(i2,a,b,c)... but the results remain the same.
I think that beacuse the saving part is at the end of the function (which runs for a long time) somehow for all the functions 'num' is 5 (the loop is finished much faster than the calculations).
any ideas? tnx
Upvotes: 0
Views: 1492
Reputation: 1901
I found a solution to the problem,
it is quite simple, all I had to do is export the variables to the enviroment and them read them in the MATLAB script
for ((i = 1; i <= 5; i++)); do
export i
matlab -nodesktop -nodisplay <script.m &> dummy.out &
done
and in the script.m
iter=str2double(getenv('i'))
myfunction(iter,a,b,c)
works good!
Upvotes: 0
Reputation: 8218
The problem is the ampersand (&
) sign after the MATLAB call - what is happening is that the loop starts running, puts the value 1 into i.txt, then forks off a MATLAB process, then the loop runs again, puts the value 2 into i.txt, then forks off another MATLAB process, and so on. Now MATLAB takes a while to start, and this loop is really quick since it's not waiting around for the MATLAB call to finish, so by the time the first MATLAB instance finally finishes, the loop is long finished and the value in i.txt is 5 for all the calls.
Short version: Remove the &
sign :)
That will make MATLAB run and finish before continuing with the loop.
Upvotes: 2
Reputation: 371
The thing is that you run jobs with the same file, as it runs in background mode, first the file contains "1", after that "2", "3", "4", "5", and only after that the first script start evaluating (and see already "5" in the file, not "1").
You are now trying to pass a parameter to a function through file, right? I just wonder, why not passing the parameter to the function itself? Running a number of functions in Matlab in parallel (in background mode) described here, for example: http://www.mathworks.ch/matlabcentral/newsreader/view_thread/166876
Upvotes: 2