Holding matlab execution while dos/system command is running

3 vues (au cours des 30 derniers jours)
olu
olu le 20 Avr 2014
Hi all,
Z (see code below) is a batch file to be executed using 'dos'. The challenge however is that V requires 'x.log' which is an output files after completely executing the batch file. What I observe is error message saying that the output file do not exist; apparently because the 'x.log' is yet to be generated by the batch file. Please How can I make matlab hold on until after the batch file has been completed so that the x.log file can be available as input to V?
z = dos('x.bat'); v = fileread('x.log')
Thank you in advance.
OlFat

Réponse acceptée

Image Analyst
Image Analyst le 20 Avr 2014
Maybe you can enter a loop until it's created
z = dos('x.bat');
% Wait for file to be created.
maxSecondsToWait = 100; % Wait 100 seconds at most.
secondsWaitedSoFar = 0;
while secondsWaitedSoFar < maxSecondsToWait
if exist('x.log', 'file')
break;
end
pause(1); % Wait 1 second.
secondsWaitedSoFar = secondsWaitedSoFar + 1;
end
if exist('x.log', 'file')
v = fileread('x.log')
else
warningMessage = sprintf('Warning: x.log never got created after waiting %d seconds', secondsWaitedSoFar);
uiwait(warndlg(warningMessage));
end
  4 commentaires
Image Analyst
Image Analyst le 7 Sep 2020
It will break from the loop as soon as the file is detected - 10 seconds in your example. By the way, instead of exist(), this is how you do it now:
if isfile('x.log')
Theodore Bantounas
Theodore Bantounas le 12 Nov 2020
Found the code useful. Thank you Img analyst.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 20 Avr 2014
dos() does wait for the end of the command. However if the command involves opening up an independent graphics window instead of working in command-line mode, then as far as MS Windows is concerned the .bat is finished. There are tools that can be used to detect whether a process is executing if necessary.
  1 commentaire
Devalla lakshmipathirao
Devalla lakshmipathirao le 7 Sep 2020
Could you please tell more about the tools you are talking about? Thanks.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by