I am trying to invoke Matlab functions from VBA and wrote a simple example code to try it out. The code tries to calculate the determinant of a random 2×2 matrix.
Sub Code()Dim DMat(1 To 2, 1 To 2) As Double, DetA As Object, Matlab As ObjectUpper = 1000Lower = 10For i = 1 To 2 For j = 1 To 2 DMat(i, j) = ((Upper - Lower) * Rnd + Lower) Sheet1.Cells(i + 1, j) = DMat(i, j) NextNextSet Matlab = CreateObject("Matlab.Application")On Error GoTo ErrorfoundMatlab.PutWorkspaceData "A", "base", DMatMatlab.Execute "Result = det(A);"Set DetA = Matlab.GetVariable(“Result”, “base”)Sheet1.Range("A6").Value = DetAExit SubErrorfound: With Err MsgBox "Source: " & .Source & vbCrLf & "Desc: " & .Description, vbCritical, "Error " & CStr(.Number) End WithEnd Sub
I get a Runtime error 440 on the line containing the GetVariable command. I have tried to use GetWorkspaceData instead of GetVariable, but the result is the same.
Where am I going wrong?
Best Answer