Matlab function in VB.NET does not work. What's wrong with it?
I decided to try COM library testing_COM.dll created by Matlab Builder NE in VB.net.
Function in Matlab:
function t = test(n)
t=sqrt(n);
end
Registered library in Windows, added reference COM in VB.net and than imported in my vb project:
Imports testing_COM
Created class variable:
Dim calcul As testing_COM.Class1
When I try to call this Matlab function test() :
MsgBox(calcul.test(16))
Instead of result "4", VB.net shows up errors:
Error 1 Argument not specified for parameter 'n' of 'Public Sub test(nargout As Integer, ByRef t As Object, n As Object)'.
Error 2 Argument not specified for parameter 't' of 'Public Sub test(nargout As Integer, ByRef t As Object, n As Object)'.
Does anybody know what is wrong with this function?
Thanks.
Re: Matlab function in VB.NET does not work. What's wrong with it?
Well, if you have a function requiring 3 parameters and only supply one parameter, what would you expect? The error message is quite specific about what's wrong.
Re: Matlab function in VB.NET does not work. What's wrong with it?
Ok, how should I call this function to get 4 from 16?
Re: Matlab function in VB.NET does not work. What's wrong with it?
First of all it's not exported as a function but as a Sub routine which doesn't return anything. If you look closely at the name of the arguments you'll see that they are named nargout, t, and n respectively. nargout is the number of out parameters the Matlab function returns, which in this case is 1. The next argument is passed byref and is named t, this will be the return value (the value is return via the argument since it's not a function), and the next is n.
Code:
function t = test(n)
t=sqrt(n);
end
Can you see what t and n is in your own Matlab code?
Code:
Dim result As Integer
calcul.test(1, result, 16)
'result should now contain the value 4.
Re: Matlab function in VB.NET does not work. What's wrong with it?
Thank You for answer.
But, unfortunately, when I tried Your code I got an error "System.NullReferenceException was unhandled" on line "calcul.test(1, result, 16)".
result=0
Re: Matlab function in VB.NET does not work. What's wrong with it?
Try declaring the result variable as System.Object instead of as an Integer, maybe the Matlab uses some other kind of data types. You should be able to afterwards cast the result to an Integer.
Re: Matlab function in VB.NET does not work. What's wrong with it?
It shows the same error, but before run underlines the "variable" in line "calcul.test(1, result, 16)" with green line with massage "Variable 'result' is passed by reference before it has been assigned a value. A null reference exception could result at runtime."
Re: Matlab function in VB.NET does not work. What's wrong with it?
Code:
Dim result As Object = New Object()
Re: Matlab function in VB.NET does not work. What's wrong with it?
I still get an error "System.NullReferenceException: Object reference not set to an instance of an object " in line "calcul.test(1, result, 16)".
Re: Matlab function in VB.NET does not work. What's wrong with it?
Can you post the code you're using.
Re: Matlab function in VB.NET does not work. What's wrong with it?
Imports testing_COM
Public Class Form1
Dim calcul As testing_COM.Class1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim result As Object = New Object()
calcul.test(1, result, 16)
End Sub
End Class
Re: Matlab function in VB.NET does not work. What's wrong with it?
Maybe it's the integer value that creates it, even if I don't understand why. But try this:
Code:
Dim result As Object = New Object()
Dim value As Object = 16
calcul.test(1, result, value)
If you're still getting a null reference error then there is something wrong with the generated COM object. Doesn't Matlab allow you to create .Net assemblies as well?
Re: Matlab function in VB.NET does not work. What's wrong with it?
there's nothing wrong with the com object... at least there wouldn't be if you actually instanciated it... you declared calcul as a tesxting_COM.Class1 ... that's fine... but you never created the instance...
Code:
Imports testing_COM
Public Class Form1
Dim calcul As testing_COM.Class1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
calcul = new testing_COM.Class1 ' <-- THIS is what you forgot to do
Dim result As Object = New Object()
calcul.test(1, result, 16) '<-- had you checked here, you would have seen that "calcul" was nothing and thus the source of the error.
End Sub
End Class
-tg
Re: Matlab function in VB.NET does not work. What's wrong with it?
With:
Dim result As Object = New Object()
Dim value As Object = 16
calcul.test(1, result, value)
error is different: "COMException was unhandled: Undefined function or method 'sqrt' for input arguments of type 'int32'.
Error in => test.m at line 2"
Re: Matlab function in VB.NET does not work. What's wrong with it?
Quote:
Originally Posted by
techgnome
there's nothing wrong with the com object... at least there wouldn't be if you actually instanciated it... you declared calcul as a tesxting_COM.Class1 ... that's fine... but you never created the instance...
Code:
Imports testing_COM
Public Class Form1
Dim calcul As testing_COM.Class1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
calcul = new testing_COM.Class1 ' <-- THIS is what you forgot to do
Dim result As Object = New Object()
calcul.test(1, result, 16) '<-- had you checked here, you would have seen that "calcul" was nothing and thus the source of the error.
End Sub
End Class
-tg
Also error: "COMException was unhandled: Undefined function or method 'sqrt' for input arguments of type 'int32'.
Error in => test.m at line 2"
Function in Matlab (test.m):
function t = test(n)
t=sqrt(n);
end
Re: Matlab function in VB.NET does not work. What's wrong with it?
Thanks everybody who tried to help me, finely I solved the problem.
Joacim Andersson gave me a clue to create NET instead of COM assembly, and I did.
Without registration in Windows (I couldn't because of error), I added in reference testing_NET.dll and MWArray.dll(for array transformation) in vb.net and changed the code to:
Code:
Imports testing_NET
Imports MathWorks.MATLAB.NET.Arrays
Imports MathWorks.MATLAB.NET.Utility
Public Class Form1
Dim calcul As New testing_NET.Class1
Dim value As New MWNumericArray(MWArrayComplexity.Real, MWNumericType.Double, 1)
Dim result
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
value=16
result = calcul.test(value)
Msgbox (result.Tostring)
End Sub
End Class
Now result=4
Everything works perfect, the only thing is that the application "freezes" for 10-12s on the line "Dim calcul As New testing_NET.Class1" when I start it.