COM interface to open Matlab from another software

I have to open and run a Matlab program when I call it from another software (Plant Simulation). I found that this has to be done with COM interface, that both supports. However I do not have any idea of how to do this. I found on Plant Simulation context help some command that can be used to control Plant Simulation from other softwares, but not viceversa. Is there a library with the commands that can be used from another software to control Matlab? If not, how can I do it? Thanks in advance

8 个评论

Thanks, I haven't found it before. I'll now study it and try to make it. Thanks!
The most relevant part of the documentation is this page.
But the fact that your other software has a COM interface does not mean that it can itself control matlab. Your other software needs to offer some form of scripting for it to control matlab.
You may find that it may be actually easier to work the other way round with matlab controlling the other software.
I have studied these document and tried to call it correctly, however it is not working. I am not sure with the registration of the Automation Server.
To do that, I run Matlab as administrator, write in the command window "regmatlabserver" and a command window is opened for few seconds and then it is closed. However, in the documentation it says "MATLAB opens a minimized command window. Open this window and exit MATLAB." This is not possible since this window is automatically closed.
Then I tried with the Prompt. Now I can open this command window and I close Matlab. Is now the registration complete? Should I do something else?
In my Plant Simulation Model I then created a COM object and tried then to execute the file that I want with this code:
var matlab : any \\ This is code from Plant Simulation
matlab:=createCOMObject("matlab.Application") \\ This is code from Plant Simulation
matlab.execute("C:\Users\****\main.m") \\ ++ are for privacy
When I run with this code, something in Matlab is opened and then closed, however the program main.m does not run and thus I do not have the results from it. What am I doing wrong?
P.S. The Plant Simulation coding part is correct (as said from an expert) and the problem is in the Matlab "part" of this code.
Thank you in advance for the help
You normally don't need to use regmatlabserver. Matlab automatically register itself when installed. The only reason to use regmatlabserver is when you have several versions installed and you don't want to use the last one installed.
What happens if in matlab you do:
matlab = actxserver('matlab.Application')
?
This should normally starts a new instance of matlab with a very basic command window instead of the full UI. Is the command window automatically closed as well?
Is there anything special in your matlabrc or startup file that could cause problem when matlab is started with no gui?
It doesn't close automatically. Never touched this two files. I didn't even know about their existance before you told me.
I noticed now that when I start Matlab is called, as said a command window is opened, but after some seconds there is the "classical" sound of error made by windows and then this window is closed.

So to be clear, in your software if you do:

matlab:=createCOMObject("matlab.Application")

you get a window that open and then is probably force-closed by the OS. But if you do:

matlab = actxserver('matlab.Application')

in matlab, the same window that appear stays up? That is very odd, the two should be equivalent.

What about if you use a vb script. Save the following in a text file with extension .vbs

Set matlab = CreateObject("Matlab.Application")
Matlab.Visible = 1

and run this script from Windows command prompt with

cscript nameofscript.vbs

Again, this should pop up the matlab window and not close it. All three are equivalent, they ask Windows to instantiate a matlab COM object.

Note: some antivirus softwares and some configurations of windows may prevent you from running vb scripts.

In the end I solved everything calling Plant Simulation from Matlab, it is way simplier. However, thank you a lot for the help!!

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Use COM Objects in MATLAB 的更多信息

产品

标签

提问:

NF
2018-4-11

评论:

NF
2018-4-20

Community Treasure Hunt

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

Start Hunting!

Translated by