Batch Files and Matlab: Sequential execution and "start \wait"

hi
I am having to call a large number, N, (hundreds) of functions from inside a bat file (see the following url for reasons why: http://www.mathworks.com/matlabcentral/answers/5973-memory-issues-almost-like-clear-doesn-t-fully-work )
I have a bat file called myBat.bat. Inside this file I have:
@echo off
start /wait /b matlab -nosplash -nodesktop -minimize -r myMatlabFile -logfile E:\Data\data\DATA\Matlab_Logging
start /wait /b matlab -nosplash -nodesktop -minimize -r myMatlabFile -logfile E:\Data\data\DATA\Matlab_Logging
start /wait /b matlab -nosplash -nodesktop -minimize -r myMatlabFile -logfile E:\Data\data\DATA\Matlab_Logging
start /wait /b matlab -nosplash -nodesktop -minimize -r myMatlabFile -logfile E:\Data\data\DATA\Matlab_Logging
and so on....
I need the files to run sequentially, else I will just end up opening N matlab threads.
I dont know much about DOS, but I thought the "/wait command" would enfore this. When I run myBat.bat I see N Matlab threads open which I dont want, as it leads to the PC crashing.
How can i force the contents of my bat file to run sequentially?
thanks

 Accepted Answer

%http://www.mathworks.com/help/techdoc/ref/system.html
function_CALLER
%Ensure there is only one copy of matlab running in the machine before you
%start...
clc;
status = false;
nInstances = inf;
while(~status)
[mem sV] = memory;
display(['The max possible array size due to RAM is : ' ...
num2str(round(mem.MaxPossibleArrayBytes/(1024*1024))) ' MB'])
[status,result] = system('matlabBatchScript.bat','-echo');
while(nInstances > 1)
[status1,result1] = system('tasklist /FI "imagename eq matlab.exe" /fo table /nh');
nInstances = numel(strfind(result1, 'MATLAB.exe'));
pause(1);
end
end
end

More Answers (5)

Do not use the DOS command "start". It enables a user to start a separate window in Windows from the Windows command line. You just need to call Matlab one at a time and quit it once it finishes processing your file.
matlab -nosplash -nodesktop -minimize -r "myMatlabFile;quit" -logfile

7 Comments

tried this and it doesnt work. It still opens up N threads (MATlAB.exe is viewable in task manager N times) This leads to the PC crashing.
have i misunderstood you?
Just to be clear there is the code "matlab -nosplash -nodesktop -minimize -r "myMatlabFile;quit" -logfile" repeated N times in one bat file.
Okay, can you modify your original line a little bit and try it again?
start /wait /b matlab -nosplash -nodesktop -minimize -r "myMatlabFile;quit;" -logfile E:\Data\data\DATA\Matlab_Logging
Just tried it with that repeated twice.
still opens up two matlab threads.
I thought /wait was supposed to enforce sequential running?
Yeah. The problem seems to be that DOS (or to be exact, Windows Command Window) regards it complete once the line of command is issued and Matlab is started. It doesn't matter whether the task in Matlab is completed or not. I understand your issue. I'll think about if there is any workaround.
There seems to be a lot of computer science stuff on the net regarding coding for loops etc in DOS and functions returning with a code once run. These look like it might be able to call the function, though I dont know.
To give you some further info, the bat file is actually calling the same function, multiple times. The function reads whats present on the disk and then deciedes what to do conditional on what is there. The point being the call is indentical each time.
Also the I currently call exit.m at the end of my function, which quits matlab. I suspect this has the same effect as your quit term?
Regarding exit and quit. Yes. Your guess is right.
see help exit
Hello I am haveing a similar problem.
What I want to do is to create a .txt file named " Travel_Time", by reading some other values from the source code in C++. These values exist in the .txt file "parameters.txt". Specifically this file contains only one line with two columns. I have initially put two values in the .txt myself in order to take them and afterwards, I have defined some values from the Matlab script to be read automatically. The results of the source code are combined to a simulation environment and are being written in a .txt file named "Mobil_MOVEMENT". Also, I have written a python script, which enables the console environment, which is named " python_script_2.py". My task is the following:
I want to run the Matlab environment in a for loop and each time call the DOS command, which will get the sixth column of the Mobil_MOVEMENT and will write to the Travel_Time some results. Each time , we put in the .parameters.txt new numbers for the two columns , that we have defined from the Matlab environment and based on them it creates again the new "Mobil_MOVEMENT and then it writes again to the new "Travel_Time" the new results. I want these results to be written one under the other for each line.
I attach you the code of Matlab DOS command.
function [times]=model()
%Parameters %p=[0.1:0.1:1]; %thr=[1:0.2:2.8];
p=[0.1 0.2];
thr=[1.0 1.2];
%Create all possible parameter pairs
[A, B] = meshgrid(p, thr);
c = cat(2, A', B');
pairs = reshape(c, [], 2);
%Results vector
minimumvalue=zeros(size(pairs, 1));
maximumvalue=zeros(size(pairs, 1));
results=zeros(size(pairs, 1));
times = zeros(size(pairs, 1));
% fileID=fopen('parameters.txt','w'); % ftemp=fopen('Travel_Time.txt','w');
for i=1:size(pairs, 1)
fileID=fopen('parameters.txt','w');
ftemp=fopen('Travel_Time.txt','w');
fprintf (fileID,'%4.2f\n%4.2f\n', pairs(i, :));
% t=cputime
[status,cmdout]=dos('"C:\Program Files\TSS-Transport Simulation Systems\Aimsun 8.0.3 (R26859)\aconsole.exe" -script python_script_2.py dutch.ang');
if (status~=0)
error('console stopped')
% times(i) = 0;
else
cmdout;
report = dlmread('Mobil_MOVEMENT.txt');
times(i) = report(:, 6);
end
minimumvalue(i)=min(times(i));
maximumvalue(i)=max(times(i));
results(i)=maximumvalue(i)-minimumvalue(i);
fprintf (ftemp,'%4.2f\n', results(i));
fclose(fileID);
fclose(ftemp);
end
My problem is that the DOS command runs only for the first parameters, that are being set in the .txt as initialization and it stops there, although I have put it in a for-loop. Also, I have created the Travel_Time.txt with a column of four zeros , as those are the maximum combinations for the vectors p and thr, that you see above.
I mean I want to get each time the line one after another the new parameters in the "parameters.txt" and the new values in the "Travel_Time.txt".
Do you know why this happens?
Any hint would be greatly appreciated

Sign in to comment.

Hang on. can i not just call the bat file in a for loop in matlab using http://www.mathworks.com/help/techdoc/ref/system.html
??
I.e. i have one copy of matlab always open and then it calls other matlab threads that open and shut after each iteration.
??
right this seems to work:
function myFunc status = false;
while(~status)
[mem sV] = memory;
display(['The max possible array size due to RAM is : ' ...
num2str(round(mem.MaxPossibleArrayBytes/(1024*1024))) ' MB'])
[status,result] = system('matlabBatchScript.bat','-echo');
pause(120);
end
end
however, the 120 is a guess.

1 Comment

I replied to the above thread with a way to get this information from the system.

Sign in to comment.

As a further refinement of your above loop, you could replace the pause with a check that symbolizes processing is still ongoing, something as simple as a file you drop on the system and then check for the existence of at some set interval. You could also include debugging information in the file, too -- what file you are processing, when processing started, progress, etc.
In my experience, the polling approach is generally superior than using a set duration pause -- since with the pause you end up either wasting time not processing or end up with too many processes running. It's also good to have sanity checks -- processing taking too long, too many processes running, memory sizes too big, etc. Pauses do have the benefit of being very straightforward to implement, though!
www.mathworks.com/support/solutions/en/data/1-16B8X explains that "matlab" should have been replaced by "matlab.bat"

Categories

Tags

Community Treasure Hunt

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

Start Hunting!