有没有办法从matlab程序中检测到一台计算机上有多少个matlab进程在运行?
我希望恰好有n个matlab进程在运行。如果我拥有的太少,我想要创建它们,如果我有很多,我想杀死一些。当然,您可以手动完成此操作,但如果可能且实现起来不太复杂,我更愿意让它自动完成。
补充信息:目前我正在使用windowsx64 (vista),但我对其他平台也很感兴趣。
发布于 2009-05-13 16:17:12
如果你使用的是Windows,你可以这样做:
[s,w] = dos( 'tasklist' );
numMatlabs = length( regexp( w, '(^|\n)MATLAB.exe' ) )发布于 2009-05-14 15:38:34
这里有另一种方法:您可以使用Matlab的COM "Automation Server“来启动工人,并从一个中央Matlab进程控制他们。
function out = start_workers(n)
myDir = pwd;
for i=1:n
out{i} = actxserver( 'matlab.application.single' );
out{i}.Execute(sprintf('cd(''%s'')', myDir));
end然后,您可以使用Execute()让它们运行工作。您可以使用计时器技巧来获得某种异步执行。
function out = evalasync(str)
%EVALASYNC Asynchronous version of eval (kind of)
%
% evalasync(str) % evals code in str
% evalasync() % gets results of previous call
persistent results status exception
if nargin == 0
out = {status results exception}; % GetWorkspaceData doesn't like structs
assignin('base', 'EVALASYNC_RESULTS', out); % HACK for Automation
return
end
status = 'waiting';
function wrapper(varargin)
status = 'running';
try
results = eval(str);
status = 'ok';
catch err
status = 'error';
exception = err;
end
end
t = timer('Tag','evalasync', 'TimerFcn',@wrapper);
startat(t, now + (.2 / (60*60*24)));
end然后
w = start_workers(3);
w{1}.Execute('evalasync(''my_workload(1)'')');
w{2}.Execute('evalasync(''my_workload(2)'')');不幸的是,你被工作线程中的单线程所困扰,所以如果你再次调用evalasync()来检查结果,它将阻塞。因此,您可能希望通过磁盘上的文件来监视它们的进度。因此,这可能算不上是一场胜利。
发布于 2009-05-13 14:37:31
在linux上
!ps -ef |grep "/usr/local/matlab78/bin/glnxa64/MATLAB"|wc -l可以做到这一点(将path替换为您自己的路径,并为grep进程减去1)
(或者要内置到函数中,请使用
[tmp, result] = system('ps -ef |grep "/usr/local/matlab78/bin/glnxa64/MATLAB"|wc -l');
str2double(result) - 1此外,您还可以使用
>>computer
ans = GLNXA64要找出程序当前在哪个系统体系结构(win/linux/等)上执行
https://stackoverflow.com/questions/858301
复制相似问题