The motivation behind this post came from an Excel – MATLAB automation problem that one of my colleagues had the previous week. I decided to write 3 small macros that will demonstrate 3 ways of using MATLAB from VBA. So, below you will find some VBA code that:

According to Wikipedia . For many engineers MATLAB is a quite useful tool that makes their everyday work easier. If you are an engineer and work on industry or academia I am sure that you have heard about it, or you might have already use it.

The idea behind the 3 macros is more or less the same;You can find more info about the MATLAB COM Automation Server here , as well as about its available functions here

the VBA code uses the MATLAB COM Automation Server in order to execute the function. Then, the function result is being further processed using the Excel/VBA Mid, Right, Len and Find functions in order to remove unnecessary spaces and, finally, get the required output.

A.

The VBA code below uses thefunction of MATLAB in order to perform numerical integration using the trapezoidal rule. So, although in Excel you might need a custom VBA function to perform numerical integration , in MATLAB environment this is done by using a simple built-in function.

'Execute the built-in MATLAB funciton - trapz(X, Y), where X and Y are arrays with the same number of elements. 'The line below actually simulates the following code: 'Result = MATLAB.Execute("trapz([0,1,2,3,4,5,6,7,8,9],[0,4,16,36,64,100,144,196,256,324])")

'In the case of error inform the user and exit the macro.

'----------------------------------------------------------------------------------------------------------------------------------- 'This macro executes a built-in MATLAB function using the MATLAB COM Automation Server. 'After running the function the result is being further processed in order to remove unnecessary spaces and get the required value. 'Here the trapz function is used as an example - trapezoidal numerical integration. 'Z = trapz(Y) computes an approximation of the integral of Y via the trapezoidal method (with unit spacing). 'To compute the integral for spacing other than one, multiply Z by the spacing increment (from MATLAB help). 'It goes without saying that in order to use this macro you must have installed MATLAB at your computer... 'Written by: Christos Samaras 'Date: 09/09/2013 'e-mail: xristos.samaras@gmail.com 'site: https://myengineeringworld.net///// '----------------------------------------------------------------------------------------------------------------------------------- 'Declaring the necessary variables.

The next code uses a custom m file that contains a simple function that calculates the area of a trapezoid based on its main dimensions (large base, small base and height).

Sub CustomFunctionOneOutput()



'-----------------------------------------------------------------------------------------------------------------------------------

'This macro executes the custom MATLAB function "TrapezoidArea" using the MATLAB COM Automation Server.

'After running the function the result is being further processed in order to remove unnecessary spaces and get the required value.



'The "TrapezoidArea" cusotm function is used for demonstration purposes.

'It calculates the area of trapezoid using the formula Area = 0.5*(LargeBase + SmallBase)*Height.



'It goes without saying that in order to use this macro you must have installed MATLAB at your computer...



'Written by: Christos Samaras

'Date: 09/09/2013

'e-mail: xristos.samaras@gmail.com

'site: https://myengineeringworld.net/////

'-----------------------------------------------------------------------------------------------------------------------------------



'Declaring the necessary variables.

Dim LargeBase As Double

Dim SmallBase As Double

Dim Height As Double

Dim Matlab As Object

Dim mFilePath As String

Dim Result As String

Dim temp As String



'Get the input values.

LargeBase = Sheet1.Range("O17").Value

SmallBase = Sheet1.Range("O18").Value

Height = Sheet1.Range("O19").Value



'Set the MATLAB object (the COM server).

On Error Resume Next

Set Matlab = CreateObject("matlab.application")

'In the case of error inform the user and exit the macro.

If Err.Number <> 0 Then

MsgBox "Could not open Matlab!", vbCritical, "Matlab Error"

Exit Sub

End If

On Error GoTo 0



'Specify the location of the m file that will be used. If the file is in a location different

'than the current workbook you can use the full path, for example:

'mFilePath = "C:UsersChristosDesktop"

mFilePath = ThisWorkbook.Path



'Load the m file in MATLAB.

Matlab.Execute ("cd('" & mFilePath & "')")



'Execute the custom function.

Result = Matlab.Execute("TrapezoidArea(" & LargeBase & "," & SmallBase & "," & Height & ")")



'Remove the unnecessary spaces from the string Result.

temp = WorksheetFunction.Substitute(WorksheetFunction.Substitute(Result, Chr(10), ""), " ", "")



'Display the function result to the user.

MsgBox "Trapezoid Area = " & Right(temp, Len(temp) - WorksheetFunction.Find("=", temp)), vbInformation, "MATLAB Result"



End Sub

C. Custom MATLAB function – two outputs

Finally, the last code uses another custom function to transform Cartesian coordinates (x, y) to polar (radius, theta angle).