MATLAB as COM Automation Server with Javascript
Show older comments
I already asked this question on StackOverflow, but this seems a better place to ask it.
Original question: https://stackoverflow.com/questions/55496623/matlab-as-com-automation-server-with-javascript
I am trying to make a connection between Matlab and a Javascript (typescript in my case) program with a COM automation server as suggested on the MathWorks website. The docs on the website have examples for some languages created by MS, not for javascript.
I can't seem to find a lot of information on how to establish such a COM connection with JS. From what I've read, it's an old Microsoft feature that was only used with Internet Explorer.
Problem
The program I am writing is a VS Code extension, thus I am not using Internet Explorer at all. As a result, I do not believe I can use ActiveXObjects.
Question
Is there another way to establish a connection between my typescript code and the Matlab instance?
Goal
I am trying to run Matlab files from the VS Code terminal without opening a custom Matlab terminal or the complete Matlab GUI. The output should be displayed in the VS Code terminal as well. On MacOS and Linux, I can simply use the CLI tools, but due to the differences between the Windows version and MacOS/Linux versions, this is not possible on Windows.
6 Comments
Siddharth Bhutiya
on 11 Apr 2019
You could run MATLAB as a COM server and then connect to it from your JS code, but I am not sure whether JS has an interface to connect to any external COM server.
From your stackoverflow post and the comments, what I understood was that, the way you are doing it in Mac and Linux, is by displaying the result on the command line. If that is the case then, on Windows, you can strucutre your MATLAB function/script to write the output to a file and then read it in your JS code.
You can use the -r startup flag to run your script as follows
matlab.exe -r "my_function();exit"
Bram Vanbilsen
on 12 Apr 2019
Guillaume
on 12 Apr 2019
I don't really understand what you're trying to do. In particular, I'm not clear who is the client and who is the server in your case.
I want to clear a few things, regarding COM (Component Object Model).
Yes, it's old technology, it's been around forever but it's still very much in use and still an essential part of Windows. It's still the only way to automate lots of products (such as the entire Microsoft Office suite) and yes it looks like Visual Studio Extensions use COM.
It has nothing to do with internet explorer. Yes, it was used extensively by IE for its plugin/automation system and was called ActiveX in this context, but COM itself and its implementation in matlab has nothing to do with IE (despite Mathworks using actx in their function name).
If the client is matlab and the server is your code, you'd use actxserver in matlab to establish the connection. Your code would have to incorporate the whole COM server machinery.
If the server is matlab and the client your code, matlab should be already registered as a COM server. so your code would have to start Matlab COM server and talk to it. In C, this involves calls to CoGetClassObject or CoCreateInstance.
Bram Vanbilsen
on 12 Apr 2019
"But now you say so, to establish the start the COM server with Matlab, Matlab probably has to be opened first. Which is the exact thing I am trying to avoid."
No, Windows will start matlab COM server for you when you request an instance from your code. You can also start the matlab COM server yourself if you want (See Manually create automation server).
Note that the COM instance is a separate instance from normal matlab. However, it does pop up a window and I'm not aware of a way to disable that. You can see the result easily from a normal instance of matlab:
m = actxserver('Matlab.Application'); %Starts matlab automation server if it's not already running
So, if you don't want to see a window this may not be useful for you.
You would also have to find out how to get a COM instance in whichever language you're using to write your extension. I know nothing about VS Code.
edit: Bear in mind that I know nothing about typescript. After a quick search, it would appear that the typescript language does not have the machinery to talk to COM components (no equivalent to actxserver above), so I don't think you can use COM from your extension anyway
Bram Vanbilsen
on 12 Apr 2019
Answers (0)
Categories
Find more on Use COM Objects in MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!