How can I stop a batch file when it is running too long in MATLAB 7.13 (R2011b)?

24 views (last 30 days)
Here’s what I’d like to do, and wondering if there’s a way to do it with TIMER in matlab--
%run m-file bla.m
>>bla
%do matlab stuff in bla.m
‘hello world’
% make a call to command line DOS batch file
! Run.bat
%if the process ‘run.bat’ gets hung up, and doesn’t finish after 10 seconds, terminate it (^c) and continue running bla.m
‘process run.bat did not finish…’
return

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 19 Jul 2012
The key point is to use SYSTEM function to use Windows command ‘start’ to run the batch file and Timer object in MATLAB to check the execution. Here is a sample code.
function bla
%%Display "hello world"
disp('hello world');
%%call to command line DOS batch file
system('start run.bat');
%%create a Timer object
% call function 'check' after 10 seconds which is defined by
% 'StartDelay' value
t = timer('TimerFcn',@check,'StopFcn',@terminate,'UserData',false,...
'StartDelay',10);
start(t);
end
%%check function will set the flag t.UserData to be true, and call
% stop function of the Timer object
function check(t,~)
t.UserData = true;
stop(t);
delete(t);
end
%%Stop function of the Timer object which terminates DOS batch file
function terminate(t,~)
if t.UserData
term = system('taskkill /im cmd.exe');
% display
fprintf('\nprocess run.bat did not finish…\n')
else
delete(t);
end
end

More Answers (0)

Tags

Products


Release

R2011b

Community Treasure Hunt

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

Start Hunting!