Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 890
Bram Vanbilsen Avatar asked Apr 03 '19 13:04

Bram Vanbilsen


1 Answers

I haven't used TypeScript very much, and what little I did, it was a long time ago when it was completely new.

However, the NPM package win32ole can be used in NodeJS, so I would assume you would be able to use it in Typescript as well (perhaps with some minor modifications to the example, or a small wrapper).

win32ole npm page

This is an example from that page, showing how to interact with Excel to create and save a worksheet.

try{
  var win32ole = require('win32ole');
  // var xl = new ActiveXObject('Excel.Application'); // You may write it as:
  var xl = win32ole.client.Dispatch('Excel.Application');
  xl.Visible = true;
  var book = xl.Workbooks.Add();
  var sheet = book.Worksheets(1);
  try{
    sheet.Name = 'sheetnameA utf8';
    sheet.Cells(1, 2).Value = 'test utf8';
    var rg = sheet.Range(sheet.Cells(2, 2), sheet.Cells(4, 4));
    rg.RowHeight = 5.18;
    rg.ColumnWidth = 0.58;
    rg.Interior.ColorIndex = 6; // Yellow
    var result = book.SaveAs('testfileutf8.xls');
    console.log(result);
  }catch(e){
    console.log('(exception cached)\n' + e);
  }
  xl.ScreenUpdating = true;
  xl.Workbooks.Close();
  xl.Quit();
}catch(e){
  console.log('*** exception cached ***\n' + e);
}
like image 176
visibleman Avatar answered Nov 11 '22 04:11

visibleman