How to issue and execute commands in the Command Window of another MATLAB instance
41 次查看(过去 30 天)
显示 更早的评论
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:
- Open a first instance of MATLAB in the usual way from the Window desktop
- From the first instance of MATLAB open a second instance of MATLAB (ideally without another user interface opening up)
- From the first instance of MATLAB tell the second instance of MATLAB to run a .m file
- Run some code on the first instance of MATLAB
- From the first instance of MATLAB tell the second instance of MATLABt to run another .m file
- 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
0 个评论
回答(1 个)
Shubham
2024-5-24
编辑:Shubham
2024-5-24
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!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!