Search code examples
windowsmatlabpid

How to get the PID of an external program (invoked by MATLAB) via MATLAB commands?


I am curious how to get the PID of an external program invoked by MATLAB (in Windows).

For instance, I invoke a notepad in MATLAB via the command !notepad.exe or system('notepad.exe'). I want to obtain the PID of this notepad immediately once it is invoked.

Since multiple notepads might be open at the same time on one computer, I need to get their respective PIDs (instead of the process name) to keep track of them. I have no clue how it can be implemented....

looking for help, thanks in advance!


Solution

  • Creation date not needed

    You can call Windows' tasklist command from Matlab using system, and then parse the results:

    name = 'notepad.exe';
    [~, s] = system(['tasklist /FI "imagename eq ' name '"']);
    result = regexp(s, ['(?<=' strrep(name, '.', '\.') '\s*)\d+'], 'match');
    result = str2double(result); % convert to numbers if needed
    

    The result of system is of the following form (two Notepad windows open; Spanish Windows version):

    >> s
    s =
        '
         Nombre de imagen               PID Nombre de sesión Núm. de ses Uso de memor
         ========================= ======== ================ =========== ============
         notepad.exe                  12576 Console                    1    13,488 KB
         notepad.exe                  13860 Console                    1    13,484 KB
        '
    

    So the regular expression searches for digits preceded by the program name and optional spaces, to give the final result

    >> result =
              12576       13860
    

    Creation date needed

    If you need to filter based on creation date, you can use Windows' wmic:

    name = 'notepad.exe';
    [~, s] = system(['wmic process where name=''' name ''' get ProcessId, CreationDate']);
    

    This gives a string such as

    s =
        'CreationDate               ProcessId  
         20191015151243.876221+120  6656       
         20191015151246.092357+120  4004       
    
         '
    

    The CreationDate is in the format yyyymmddHHMMSS+UUU, where +UUU or -UUU is number of minutes from UTC.

    You can parse s into a cell array of strings as follows:

    result = reshape(regexp(s, '[\d+\.]+', 'match').', 2, []).'; % 2 is the number of columns
    

    This gives

    result =
      2×2 cell array
        {'20191015151243.876221+120'}    {'6656'}
        {'20191015151246.092357+120'}    {'4004'}
    

    Then you can filter based on the first columnn.