|
-
May 12th, 2013, 02:22 PM
#1
Thread Starter
New Member
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.
Last edited by Reprod; May 12th, 2013 at 04:10 PM.
-
May 12th, 2013, 04:14 PM
#2
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.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 12th, 2013, 04:18 PM
#3
Thread Starter
New Member
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?
-
May 12th, 2013, 05:59 PM
#4
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.
-
May 13th, 2013, 05:02 AM
#5
Thread Starter
New Member
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
-
May 13th, 2013, 05:15 AM
#6
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.
-
May 13th, 2013, 07:25 AM
#7
Thread Starter
New Member
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."
-
May 13th, 2013, 08:24 AM
#8
Re: Matlab function in VB.NET does not work. What's wrong with it?
Code:
Dim result As Object = New Object()
-
May 13th, 2013, 09:44 AM
#9
Thread Starter
New Member
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)".
-
May 13th, 2013, 11:32 AM
#10
Re: Matlab function in VB.NET does not work. What's wrong with it?
Can you post the code you're using.
-
May 13th, 2013, 12:42 PM
#11
Thread Starter
New Member
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
-
May 13th, 2013, 01:25 PM
#12
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?
-
May 13th, 2013, 01:34 PM
#13
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
-
May 13th, 2013, 01:40 PM
#14
Thread Starter
New Member
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"
-
May 13th, 2013, 01:52 PM
#15
Thread Starter
New Member
Re: Matlab function in VB.NET does not work. What's wrong with it?
 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
Last edited by Reprod; May 13th, 2013 at 02:20 PM.
-
May 16th, 2013, 12:33 PM
#16
Thread Starter
New Member
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.
Last edited by Reprod; May 16th, 2013 at 01:30 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|