How to detect which instance of MATLAB I am working from

11 Ansichten (letzte 30 Tage)
David
David am 22 Nov. 2017
Kommentiert: Grace Kepler am 18 Aug. 2025
I currently have 7 instances of MATLAB open on my PC running long simulations. They are all running the same functions with different inputs. To ensure there is no accidental interaction between instances I have temporary folders that each instance uses for data input and output before saving the final result elsewhere. I currently input an instance value (1-7) by hand for each MATLAB instance to tell them apart but can I get MATLAB to work out which instance it is automatically?

Akzeptierte Antwort

Jan
Jan am 22 Nov. 2017
Bearbeitet: Jan am 22 Nov. 2017
As far as I understand, it is not defined anywhere, what the "instance" is. Then Matlab cannot detect it automatically.
Start with defining a key to recognize the "instance". Use e.g. the PID, the process ID provided by the operating system.
feature('getpid')
Then you need e.g. a file, which contains the relation between the PID and the instance. But closing and restarting a Matlab session results in a new or old PID, such that this helps only to identify the current set of Matlab sessions.
Another option would be to create a UUID to identify a Matlab session. See e.g. https://www.mathworks.com/matlabcentral/fileexchange/21709-uuid-generation or:
jUUID = java.util.UUID.randomUUID;
cUUID = char(jUUID.toString);
>> a8a4eae2-6cc7-47d3-b58e-408b71fab260
This is such random that it can be considered to be unique. Well, storing this e.g. in the ApplicationData of the groot object might be useful to identify a Matlab session securely, but you wanted a simple counter for the instance, as far as I understand.
1. What about starting the Matlab session with defining the instance number automatically:
for k = 1:7
system(sprintf('matlab -r "setappdata(groot, ''Instance'', %d)"', k)
end
This can be done from a bash or batch script also. Then you can request the instance easily from the code:
myInstance = getappdata(groot, 'Instance')
2. You could request, how many other Matlab instances are running already. This could be done in startup.m:
[status, str] = system('tasklist'); % Assuming Windows, use 'ps -eaf' on Linux
List = strsplit(str, '\n');
nMatlab = sum(strncmp(List, 'matlab.exe', 10));
% Now store nMatlab in groot as above
But again you get a problem, if you e.g. stop 2 Matlab sessions and open a new one. Then you have a duplicate instance number. So version 1 is more reliable.
  3 Kommentare
Jan
Jan am 23 Nov. 2017
There might be a racing condition, if you start several Matlab sessions at nearly the same time: The temporal order of event can be:
session 1: read file
session 1: find that [n] is free
session 2: read file
session 2: find that [n] is free
session 2: write [n] to the file
session 1: write [n] to the file
It is not trivial to block the execution of the file until one instance has finished the process of reading and writing. The startup of Matlab can take 10 to 60 seconds, depending on the speed and load of the hard drive and network. Therefore there is a large time slot, which is susceptible for racing conditions.
With option 1 (define the instance key by -r flag at startup) the caller takes the role of the main thread and distributes the numbers securely to all children.
There must be a secure way to provide mutexes (mutual exclusive access to a shared resource). You can create such a mutex e.g. in the operating system. It is easy to share them between multiple threads, e.g. started inside a C-Mex function, but I do not know a way to share it between e.g. different Matlab sessions. It would be nice, if such a tool uses the TCP/IP for the communication, such that you could call it through the network also and the session need not run on the same machine.
@All readers: Do you know a tiny executable, which manages mutexes through TCP/IP?
Some times ago Walter has explained exhaustively, that the methods provided by the different file systems for exclusive access are not 100% secure.
Grace Kepler
Grace Kepler am 18 Aug. 2025
If you use the process ID (PID) to identify different MATLAB instances, please be aware of the following information.
Starting in R2025a, you can obtain the PID of a MATLAB process with the MATLAB command:
>> matlabProcessID
This function returns the PID as a uint64 variable.
In earlier versions of MATLAB, you can obtain the PID of a MATLAB process with the MATLAB command:
>> feature('getpid')
This returns the PID as a double variable. Please note that the use of the "feature" function is undocumented, meaning that it can be removed or changed at any time.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by