This post will demonstrate using MATLAB code to connect with Origin, read data from the active worksheet in Origin, process the data, and place the results back into the worksheet in Origin.
1. Download and open the OGW file( partial_least_regression_missing) in Origin and activate the first worksheet in the book. This sheet has data with missing values.
2. Launch MATLAB. Download and run partial_least_regression_missing_value_imputation. Missing values in the Origin worksheet will be replaced by imputed values calculated in MATLAB. All the operations in the Origin workbook template is updated if auto calculation is selected in the analysis dialog.
In this example, MATLAB acts as an client application that connects with Origin. A COM object for Origin application is created, and then data is exchanged back and forth with Origin. In addition to data, commands can be sent to Origin, and methods and properties of Origin objects can also be accessed.The MATLAB code and comments for this application is listed below:
% Obtain Origin COM Server object
% Connect to an existing instance of Origin
% or create a new one if none exists
originObj = actxserver('Origin.ApplicationSI');
% Make the Origin session visible
invoke(originObj, 'Execute', 'doc -mc 1;');
% Clear "dirty" flag in Origin to suppress prompt
% for saving current project
invoke(originObj, 'IsModified', 'false');
%locate the active workbook in current project
strBook = invoke(originObj, 'ActivePage');
BookName = invoke(strBook, 'Name');
% point to the active worksheet, or you can specify your own workbook
% by BookName.
wks = invoke(originObj, 'FindWorksheet',BookName);
%get data into the worksheet, (0,0,-1,0) specifies the start row, start
%column, end row, end column in the worksheet.
x = cell2mat(invoke(wks,'GetData', 0,1,-1,43));
%missing data imputation by Matlab Bioinformatics Toolbox
imputed = knnimpute(x);
% Import fitting results into the worksheet
invoke(wks, 'SetData', imputed, 0, 1);
Note: If you got an error message after run MatLab script as “Undefined function ‘invoke’ for input arguments of type ‘double’.”, please clean windows registry first with the following instructions.
- Click your Window’s Start menu and type “regedit” and press enter to run Registry editor.
- Find HKEY_CURRENT_USER\SOFTWARE\OriginLab folder. Right click and delete it.
- After that please reopen Origin and follow the instruction of the blog again, that error should not be happen.
In case the error happens again, please clean registry and restart your computer.