0

Anyone knows how to do MATLAB COM autiomation in VB.NET? Since I really can't make my program works using the NE builder. I tried using the COM automation as documented here: http://www.mathworks.com/help/matlab/matlab_external/call-a-matlab-function-from-visual-basic-net-client.html

Again, my program is so simple. Here's the matlab code:

function out = addMe(a,b)
out = a + b;
end

Here's the VB code:

Public Class Form1
    Dim a As Integer = 4
    Dim b As Integer = 10
    Dim result As String
    Dim Matlab As Object

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Matlab = CreateObject("Matlab.Application")
        result = Matlab.Execute("cd C:\Users\Elvin Gentiles\Desktop\Program")
        result = Matlab.Execute("addMe(a,b)")
        TextBox1.Text = result
    End Sub
End Class

The result that I'm getting that is showing in the textbox is: ??? Undefined function or variable 'a'. I already made sure that the COM reference is already added.

But when I tried changing the code to this it is working. By the way, version is a matlab command used to show the version of the MATLAB.

result = Matlab.Execute("version")

I hope you can help me with this. I really needed this. Thanks

elvinguitar
  • 149
  • 3
  • 6
  • 15

1 Answers1

2

Everythings working perfect - COM-wise.

Ask yourself: what did you expect the function to return?

If the code above is complete, you defined neither a nor b in the matlab-session, so matlab of course complains about a not being defined.

Try

result = Matlab.Execute("addMe(1,2)")

instead.

sebastian
  • 9,526
  • 26
  • 54
  • what if I want to pass the value of a variable from VB to MATLAB? BY the way, I tried your suggestion and the returned answer is **??? Undefined function 'addMe' for input arguments of type 'double'.** – elvinguitar Jan 30 '14 at 13:38
  • Did you use Matlab ever before? The error message is pretty self-explanatory. You'll have to make sure the `addMe` function is on the path - I kind of assumed that `addMe.m` would be in the directory you `cd` to. You can do `Matlab.Execute("a = 2")` etc. or check http://www.mathworks.de/de/help/matlab/call-matlab-com-automation-server.html (`PutFullMatrix`, `PutWorkspaceData`) on how two write matrices more directly to matlab. – sebastian Jan 30 '14 at 13:45