How can I stop a batch file when it is running too long in MATLAB 7.13 (R2011b)?
2 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
MathWorks Support Team
2012-7-19
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
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!