Invoking Matlab function from VBA

8 ビュー (過去 30 日間)
VR
VR 2018 年 10 月 10 日
編集済み: Kevin Chng 2018 年 10 月 11 日
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 2x2 matrix.
Sub Code()
Dim DMat(1 To 2, 1 To 2) As Double, DetA As Object, Matlab As Object
Upper = 1000
Lower = 10
For i = 1 To 2
For j = 1 To 2
DMat(i, j) = ((Upper - Lower) * Rnd + Lower)
Sheet1.Cells(i + 1, j) = DMat(i, j)
Next
Next
Set Matlab = CreateObject("Matlab.Application")
On Error GoTo Errorfound
Matlab.PutWorkspaceData "A", "base", DMat
Matlab.Execute "Result = det(A);"
Set DetA = Matlab.GetVariable(“Result”, “base”)
Sheet1.Range("A6").Value = DetA
Exit Sub
Errorfound:
With Err
MsgBox "Source: " & .Source & vbCrLf & "Desc: " & .Description, vbCritical, "Error " & CStr(.Number)
End With
End 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?
  3 件のコメント
VR
VR 2018 年 10 月 11 日
Kevin, Thank you. This worked ! I used GetFullMatrix instead of GetVariable.
Kevin Chng
Kevin Chng 2018 年 10 月 11 日
Ya. Do you mind accept my answer?

サインインしてコメントする。

採用された回答

Kevin Chng
Kevin Chng 2018 年 10 月 11 日
編集済み: Kevin Chng 2018 年 10 月 11 日
Very interesting. I never try out this way in VBA to call MATLAB, normally, I follow MATLAB spreadsheet link.
Or you try GetFullMatrix, dim your variable as double.
remove set as it is not object anymore.
I guess it is because of the data type conversion. I faced it when I am using the MATLAB Api for python.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Export to MATLAB についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by