Batch Files and Matlab: Sequential execution and "start \wait"
Show older comments
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
More Answers (5)
Fangjun Jiang
on 21 Apr 2011
0 votes
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
mathworks2011
on 21 Apr 2011
Fangjun Jiang
on 21 Apr 2011
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
mathworks2011
on 21 Apr 2011
Fangjun Jiang
on 21 Apr 2011
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.
mathworks2011
on 21 Apr 2011
Fangjun Jiang
on 21 Apr 2011
Regarding exit and quit. Yes. Your guess is right.
see help exit
Antonios Georgantas
on 21 Jun 2016
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
mathworks2011
on 22 Apr 2011
0 votes
mathworks2011
on 22 Apr 2011
1 Comment
Jason Ross
on 22 Apr 2011
I replied to the above thread with a way to get this information from the system.
Jason Ross
on 22 Apr 2011
0 votes
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!
Mdaatniel
on 11 Dec 2012
0 votes
www.mathworks.com/support/solutions/en/data/1-16B8X explains that "matlab" should have been replaced by "matlab.bat"
Categories
Find more on Execution Speed in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!