MATLAB Answers

How can I get the process ID of the current MATLAB session in MATLAB 7.5 (R2007b)?

77 views (last 30 days)
I want to find out the process ID of the current MATLAB session programmatically on Windows and Unix machines.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 15 Nov 2015
While there is no single command in MATLAB that can obtain the process ID of the current session of MATLAB, to achieve this you can use the shell commands TASKLIST and PSLIST.
The MATLAB central files located in the following section, use the above shell commands to yield the current lists of tasks running on a machine :
You can download the above files, and use them in the following way from within MATLAB to obtain the process ID's of all the current MATLAB sessions:
1) Windows XP:
s = tasklist;
for i = 1:size(s,2)
cmp(i) = strcmp(s(1,i).name,'MATLAB.exe');
end
mpid = s(find(cmp==1)).pid
2) Windows (NT and higher) and LINUX:
s = pslist('MATLAB.EXE')
mpid = s.pid
Note that to use the PSLIST command in Windows, you need to download pslist.exe, which is a freeware.
Note that MathWorks does not control the content posted by visitors to MATLAB Central, and does not guarantee the accuracy or reliability of such content.

  1 Comment

Oliver Woodford
Oliver Woodford on 23 Dec 2015
I believe this will only work if there is only one instance of MATLAB running. It isn't useful if you are running multiple instances, and want the process ID of the particular instance, because it cannot distinguish between them.

Sign in to comment.

More Answers (1)

Sign in to answer this question.

Products


Release

R2007b