How can I write a function in VBA which uses data of cells in Excel, work with this data in MATLAB and return a value to Excel?

12 views (last 30 days)
I want to write a function in VBA which uses data of an Excel sheet and evaluate a function in MATLAB with this data as input parameter. Afterwords the evaluated result should be returned back to Excel into a specific cell. I found the functions in the documentation, but not a simple step by step example of what I have to do.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 10 Sep 2012
This example has been included in the documentation for MATLAB 7.5 (R2007b).
For previous product releases, read below:
The attached file contain the following:
1) matlabtest1.m
MATLAB function example, which multiplicate the data matrix with itselfs
2) test1.xls
Excel sheet with Macros for using the function
This sample of VBA code assumes that you have configured Excel to work with Excel Link. If not, please see the documentation section about "Installing and Operating Excel Link".
In particular, this example requires that:
1. The MATLAB application has been initialized via "startmatlab" or "matlabinit"
2. In Excel, Excel Link is checked as an Add-In (Tools -> Add-Ins)
3. In Excel VB Editor, Excllink is checked as a reference (Tools -> References).
Here is a step by step procedure to write a simple function in VBA based on this example:
1) Configure Excel sheet
a) Excllink is checked as a Add-In
Tools --> Add-In
Use the correct Excel Link Version related to your MATLAB Version
b) Define Data with a name in Excel:
Write data i.e. into cells A1:A3, mark this area and define a name:
Insert --> Name -> Define type in "DATEN"
c) Open VBA Editor
Tools --> Macro --> Visual Basic Editor
2) CONFIGURE VBA
a) Excllink is checked as a reference
Tools --> References
Search for file "excllink.xla" in your $MATLAB\toolbox\exlink directory, where $MATLAB is your used MATLAB installation directory. The Version of MATLAB and the related Excel Link has to be the same as your add-in in Excel.
b) Insert new Module
Insert --> Module
3) WRITE SUB IN EXCEL VB EDITOR (here based on attached example)
Notes:
- "MLPutMatrix" puts the data into MATLAB workspace
- "MLEvalString" evaluates String in MATLAB, where "matlabtest1" is the MATLAB function in this example
- "MLGetMatrix" write value of variable v into the target cells
Look at the following links for more information about available functions:
Functions - By Category (Excel Link)
<http://www.mathworks.com/help/exlink/functionlist.html>
What the Functions Do :: Getting Started (Excel Link)
<http://www.mathworks.com/access/helpdesk/help/toolbox/exlink/f2-2311.html>
Sub CommandButton1_Click()
MLPutMatrix "data", Range("B3:C6")
MLEvalString "y = data + 1"
MLGetMatrix "y", Range("E3:F6").Address
MatlabRequest
MLEvalString "y1 = data*data'"
MLGetMatrix "y1", Range("B8:E11").Address
MatlabRequest
MLEvalString "y2 = matlabtest1(data)"
MLGetMatrix "y2", Range("B13:E16").Address
MatlabRequest
End Sub
3) Specify macro for button in Excel sheet related to this SUB:
test1.xls!CommandButton1_Click
c) Save everything and try out (push at the button)
For more information about examples of using Excel Link functions from VBA code in the Excel VB Editor, rather than from an Excel spreadsheet see related solution.

More Answers (0)

Categories

Find more on Data Export to MATLAB in Help Center and File Exchange

Products


Release

R2006b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!