Detect matlab processes from within matlab
If you're on Windows, you could do this:
[s,w] = dos( 'tasklist' );
numMatlabs = length( regexp( w, '(^|\n)MATLAB.exe' ) )
Here's another approach: you could use Matlab's COM "Automation Server" to start up workers and control them from a central Matlab process.
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
Then you can use Execute() to have them run work. You could use a timer trick to get sort of asynchronous execution.
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
Then
w = start_workers(3);
w{1}.Execute('evalasync(''my_workload(1)'')');
w{2}.Execute('evalasync(''my_workload(2)'')');
Unfortunately, you're stuck with single-threading in the workers, so if you call evalasync() again to check the results, it'll block. So you'd want to monitor their progress through files on disk. So it may not be much of a win.
on linux
!ps -ef |grep "/usr/local/matlab78/bin/glnxa64/MATLAB"|wc -l
would do the trick (replace path by your own and subtract 1 for the grep process)
(or to build in to a function, use
[tmp, result] = system('ps -ef |grep "/usr/local/matlab78/bin/glnxa64/MATLAB"|wc -l');
str2double(result) - 1
also, you can use
>>computer
ans = GLNXA64
to find out which system architecture (win/linux/etc ) a program is currently executing on