How to opening 1 instance of MATLAB from MATLAB, but accessing it multiple times

Hello,
I would like to do the followings:
  1. From a MATLAB session, open another instance of MATLAB - which can be done by: !matlab &
  2. Every now and then from existing MATLAB session have a command executed in the other session. This is the part that I do not know how to do. How in existing MATLAB session, I can have a command exscuted on another instance I already opened.
I greatly appreciate your suggestions.

3 Comments

Which operating system? The techniques differ with operating system and I do not feel like writing them all up.
Thanks for immediate response.
OS: Micorsoft Windows 10 Enetrprise
Does the second MATLAB have to have its Desktop visible to interact with or does it suffice to be running in "headless" mode?
Do you have the Parallel Computing Toolbox?

Sign in to comment.

 Accepted Answer

Using the Parallel Computing Toolbox, look at parpool and parfeval. parpool will start a N number of "workers" (i.e. headless instances of MATLAB), which can be running either on your local machine (supported out of the box) or on a remote machine (additional work to setup). parfeval offloads execution to a worker. For example
% Initialization: get a handle to the cluster and start a pool of 1 worker
cluster = parcluster;
pool = cluster.parpool(1);
% ... run other code ...
% Spawn command to run on worker
pool.parfeval(@mycode,0,{});
parfeval is non-blocking and can be called whenever you want to offload code. The {} is the input arguements to calling mycode. If you code has input args, assign them into {}.
If your code returns a result, then you'll need to assign it a result so that you can pull the result(s) back. You won't be notified when the code finishes, so you'll need to verify that the state has finished.
f = pool.parfeval(@mycode,1,{});
% ... run other code ...
% Get results
f.wait
result = f.fetchOutputs;
See the doc for parfeval for more information. One consideration is that a pool will expire if not used after a period of time (default is 30 minutes). You can disable this auto-deletion in the parallel preferences.

6 Comments

It is debatable whether this meets the requirements that a different matlab instance be used. Yes from some perspectives, No from other perspectives...

It might solve the actual needs though!

Hello Raymond and Walter,
Thanks for your help.
I look into getting a license to Parallel Computing Toolbox.
One question (this time assume I have the Parallel Computing Toolbox)):
If I start 1 "worker" and then I set that "worker" doing an indefinite loop (e.g., pulling data from a USB dvice into workspace or save to a file) using parfeval. Then I continue doing some other calculations and actions in current MATLAB until I am ready to end the indefenite loop on "worker", how do I end or interrupt the "indefeinite loop" in "worker"?
Thanks.
To end the indefinite loop, you could cancel() the future. I am not certain whether onCleanup() will work in such a case.
If onCleanup() will not work while canceling a future, then possibly the easiest way would be to use a memory map https://www.mathworks.com/help/matlab/ref/memmapfile.html to write a terminate request (the worker would have to be checking the file periodically)
To be able to have the worker send data to the controlling process in real-time, have the controlling process first start a parallel data queue https://www.mathworks.com/help/parallel-computing/parallel.pool.dataqueue.html or the pollable version of that, and make sure it gets passed to the worker process. When the worker queues data to the construct, the controlling process can pull it out.
This is a one-way queue. The method of building a reverse queue (to allow the controlling process to send data to the worker) is a bit awkward but can be done.
Correct, use the cancel() method to end the task (not interrupt). For example
>> local = parcluster('local');
>> pool = local.parpool(1);
Starting parallel pool (parpool) using the 'local' profile ...
Connected to the parallel pool (number of workers: 1).
>>
>> % Run task for 10 hours
>> f = pool.parfeval(@(t)pause(t),0,60*60*10);
>> f.State
ans =
'running'
>> % Cancel task preemptively
>> f.cancel
>> f.State
ans =
'finished'
>>
Thank you very much for all the valuable & useful help.
Have a pleasant time.

Sign in to comment.

More Answers (2)

Hello,
Thanks.
Answering your questions:
Q1. Does the second MATLAB have to have its Desktop visible to interact with or does it suffice to be running in "headless" mode?
A1. It can be "headless" and run in background. No interaction via desktop needed.
Q2. Do you have the Parallel Computing Toolbox?
A2. No. However, I would like to know how to do this in both options (with and without Parallel Computing Toolbox ).
Many thanks for the help.
As you are using Windows, one approach is to use .NET System.Diagnostics.Process https://www.mathworks.com/matlabcentral/answers/586706-how-to-redirect-standard-input-and-standard-output-using-net-system-diagnostics-process#answer_487475 to open a process connection, and write commands to the other MATLAB. The link shows how to redirect input and output.
Another possibility is to invoke MATLAB through actxserver() and use the Automation Engine facilities; https://www.mathworks.com/help/matlab/call-matlab-com-automation-server.html

1 Comment

I am trying to do something similar to what the OP has asked about. I do not have the Parallel Processing toolbox, and so was interested in your alternative suggestions.
First let me describe what I would like to accomplish. I am running MATLAB (R2024a) Update 2 on a Windows 10 computer.
  1. Open a first instance of MATLAB in the usual way from the Window desktop
  2. From the first instance of MATLAB open a second instance of MATLAB (ideally without another user interface opening up)
  3. From first instance of MATLAB tell second instance of MATLAB to run a .m file
  4. Use the first instance of MATLAB interactively for some time (1 hour or more)
  5. From the first instance of MATLAB tell the second instance of MATLABt to run a .m file
  6. From the first instance of MATLAB tell the second instance of MATLAB to run the quit command
I see that you suggest that this could be done using either a com server or .Net. I looked into the com server approach, but will have difficulties with that, because in our corporate installation, it is difficult to obtain needed administrative privileges.
I am therefore most interested in the .Net approach. Googling this I see some general mentions that this could be used for my use case, but as I don't know anything about .Net I'm not really sure where to start.
If you could provide any further specific hints, or useful links that specifically address my main need which is to tell the second instance of MATLAB to run a .m file I would be very appreciative. Thanks

Sign in to comment.

Products

Release

R2021a

Asked:

on 17 Aug 2021

Commented:

Jon
on 9 May 2024

Community Treasure Hunt

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

Start Hunting!