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); release(originObj);
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.
8 Comments on “Accessing Origin as server application from MATLAB”
hello,how can i set the plot properties like line width and type using matlab,thank you
Hello Adam, Thanks for your question. You will need to call Labtalk script to do so. Please see details here:
If you have further questions, please email us at email@example.com.
Hello，I have a question.
The example give the property(or method), such as ‘Name’, ‘GetData’, ‘SetData’.
And I want to set the other property, such as ‘Long name’, ‘Units’, etc.
How can I get the property list or the grammar form？
I have tried the ’get‘ function of matlab. It seems not working.
Please try the following code to set longname, Units, etc.
cols = invoke(wks, ‘Columns’);
colx = invoke(cols, ‘Item’, uint8(0));
invoke(colx, ‘Longname’, ‘Length’);
invoke(colx, ‘Units’, ‘m’);
More properties of column and its usage can be found in the following page:
Hi, I would like to ask. I still got this error after for a few times. How should i resolve this problem?
x = cell2mat(invoke(wks,’GetData’, 0,0,-1,-1));
Undefined function ‘invoke’ for input arguments of type ‘double’.
Hello, Sorry for replying late. Here is what we found. I will also update the blog.
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.
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.
I follow your instruction deleting the folder in regedit and restart the computer but still get the error. In the regedit the folder is added on by itself again. How can I solve it?
Can you let us know the version of your Origin and Matlab? Also are you running virtual machine?