How to interact with Origin/OriginPro from within MATLAB

Did you know that you can send data and commands to a running session of Origin directly from MATLAB using Origin's Automation/COM Server support? This is not a trivial task, though, and requires some trial and error to get it working. Here are a couple of guides that got me started:

- An example of how to create a plot in Origin from MATLAB can be found by going to the following sub-folder in your Origin installation directory: ..OriginRoot\Samples\Automation Server\MATLAB. Open and run the file CreatePlotInOrigin.m.

- OriginLab has a Wiki containing some of the operations that can be performed on a running OriginCOM server. Most of the examples provided are not aimed for MATLAB users, though.

- You might also want to take a look at OriginLab's forum for the COM Automation Server.

Connect to Origin from MATLAB

First you must launch an OriginCOM Server. This is done using MATLAB's actxserver:

originObj = actxserver('Origin.ApplicationSI');

originObj is now the handle to the running instance of Origin. Now, use MATLAB's invoke to send calls to Origin through the created OriginCOM server. For example, this command makes the Origin GUI visible to the user

invoke(originObj, 'Execute', 'doc -mc 1;');

Here we tell MATLAB to execute the LabTalk command 'doc -mc 1;' in Origin. You can find more methods that can be invoked here.

Get information from Origin

You can use MATLAB's function get to obtain information from the running Origin session. This is information such as data stored in the workbook sheets and the plotted figures. Origin elements, such as workbooks, worksheets, figures, etc. are accessed by first creating a handle to them. For example, a handle to all workbooks in the Origin session is obtained using

workbooksHandle = invoke(originObj,'WorksheetPages');

With this handle we can get information about the workbooks such as the their total number:

nbooks = get(workbooksHandle,'Count');

A useful source of calls is found in my contribution to the MATLAB file exchange called importOrigin. This function identifies and imports pairs of (X,Y) column data from an existing Origin project and into MATLAB. It works by using a series of loops running systematically through all workbooks, all sheets in each workbook, and all columns in each sheet. X-Y pairs are found by exploiting that, in Origin, a Y-column is automatically coupled to the X-column on its nearest left hand side in the sheet.

Defining X-Y pairs of column data is convenient in spectral data analysis, which Origin is also widely used for. The functionality for importing spectra directly from Origin and into a|e (my UV-Vis-IR freeware) is based on importOrigin.

Send information to Origin

To be updated...