Clear Filters
Clear Filters

How to issue and execute commands in the Command Window of another MATLAB instance

67 views (last 30 days)
I would like to have two instances of MATLAB running on my computer with some interaction between the two instances.
I am running MATLAB (R2024a) Update 2 on a Windows 10 computer.
Specifically I would like to:
  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 the first instance of MATLAB tell the second instance of MATLAB to run a .m file
  4. Run some code on the first instance of MATLAB
  5. From the first instance of MATLAB tell the second instance of MATLABt to run another .m file
  6. From the first instance of MATLAB close the second instance of MATLAB e.g. run the quit command
I have seen in other posts 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. Also, I do not have the Parallel Processing Toolbox, so I can not use approaches that use that.
Following from some other related postings, e.g. ( https://www.mathworks.com/matlabcentral/answers/586706-how-to-redirect-standard-input-and-standard-output-using-net-system-diagnostics-process?s_tid=prof_contriblnk) I tried doing running this Hello World example from the first instance of MATLAB
NET.addAssembly('System')
matlabProcess = System.Diagnostics.Process()
matlabProcess.StartInfo.FileName = 'matlab'
matlabProcess.StartInfo.Arguments = '-nosplash'
matlabProcess.StartInfo.UseShellExecute = false;
matlabProcess.StartInfo.RedirectStandardInput = true;
matlabProcess.StartInfo.RedirectStandardOutput = true;
matlabProcess.Start();
matlabProcess.StandardInput.WriteLine("disp('Hello from MATLAB')")
This sucessfully opens the second MATLAB instance, but no commands (e.g. disp('Hello from MATLAB') appear in the command window of the second instance.
I know that there are simpler ways to launch a second instance of MATLAB and have it run just one command, however I need to start some processes running on the second instance, use the results of these in the first instance, and then thave the second instance run some code to gracefully stop some processes and shut down.
I posted an earlier version of this inquiry as a comment to the related question https://www.mathworks.com/matlabcentral/answers/1435767-how-to-opening-1-instance-of-matlab-from-matlab-but-accessing-it-multiple-times, but am now posting it here as its own question, along with some specific code that I tried, so it will reach others that might not be following that older question.
Thanks for any suggestions you can provide.
Best Wishes,
Jon

Answers (1)

Shubham
Shubham on 24 May 2024
Edited: Shubham on 24 May 2024
Hey Jon,
You may find the following MATLAB Answers cases relevant for using the .NET interface:
Essentially you are trying to spawn a MATLAB process and then control it. It seems you are explicitely implementing the parallel computing without the toolbox. In such a case you may try the "Parallel Matlab Interface" available on file exchange:
The following MATLAB Answer case also discusses about parallelization without the parallel computing toolbox:
MATLAB also provides interfaces for other languages such as C/C++. You may try following your workflow by using such external interfaces and package them as MEX files.
Lastly, you may try to incorporate IPC between two instances of MATLAB for sharing results of one instance to another. Have a look at the following resources which could prove helpfult to you:
I hope this helps!
  1 Comment
Jon
Jon on 3 Jun 2024
Sorry, I just saw your answer. Thanks for researching this for me. I had found some of those, but could not find an exact fit to what I was doing. I will look at the all the references that you have suggested again though to see if there is something I can use for my situation.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!