我创建了一个小的MATLAB-GUI来选择目录,并通过单击按钮在该目录中启动外部MATLAB脚本。脚本的路径保存在变量file
中,我用run(file)
启动它。但是现在我想通过点击另一个按钮来停止这个脚本。有没有人知道如何做到这一点?
答案 0 :(得分:1)
如果您不想对要调用的脚本进行任何更改,可以尝试在新的Matlab实例中运行脚本,然后在要停止脚本运行时终止该matlab进程。类似的东西:
curl -u admin:admin123 -H "Accept: application/json" http://localhost:8081/nexus/service/local/repositories/snapshots/content/org/foo/project/1.0.0-SNAPSHOT/
oldPids = GetNewMatlabPIDs({}); % get a list of all the matlab.exe that are running before you start the new one
% start a new matlab to run the selected script
system('"C:\Program Files\MATLAB\R2012a\bin\matlab.exe" -nodisplay -nosplash -nodesktop -minimize -r "run(''PATH AND NAME OF SCRIPT'');exit;"');
pause(0.1); % give the matlab process time to start
newPids = GetNewMatlabPIDs(oldPids); % get the PID for the new Matlab that started
if length(newPids)==1
disp(['new pid is: ' newPids{1}])
elseif length(newPids)==0
error('No new matlab started, or it finished really quickly.');
else
error('More than one new matlab started. Killing will be ambigious.');
end
pause(1);
% should check here that this pid is still running and is still
% a matlab.exe process.
system(['Taskkill /PID ' newPids{1} ' /F']);
从系统命令GetNewMatlabPIDs
获取Matlab.exe的PID:
tasklist