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

22 vues (au cours des 30 derniers jours)
mathworks2011
mathworks2011 le 21 Avr 2011
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

Réponse acceptée

mathworks2011
mathworks2011 le 22 Avr 2011
Modifié(e) : Walter Roberson le 26 Sep 2018
%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

Plus de réponses (5)

Fangjun Jiang
Fangjun Jiang le 21 Avr 2011
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 commentaires
Fangjun Jiang
Fangjun Jiang le 21 Avr 2011
Regarding exit and quit. Yes. Your guess is right.
see help exit
Antonios Georgantas
Antonios Georgantas le 21 Juin 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

Connectez-vous pour commenter.


mathworks2011
mathworks2011 le 22 Avr 2011
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.
??

mathworks2011
mathworks2011 le 22 Avr 2011
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 commentaire
Jason Ross
Jason Ross le 22 Avr 2011
I replied to the above thread with a way to get this information from the system.

Connectez-vous pour commenter.


Jason Ross
Jason Ross le 22 Avr 2011
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
Mdaatniel le 11 Déc 2012
www.mathworks.com/support/solutions/en/data/1-16B8X explains that "matlab" should have been replaced by "matlab.bat"

Catégories

En savoir plus sur Application Deployment dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by