Results 1 to 16 of 16

Thread: Matlab function in VB.NET does not work. What's wrong with it?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    9

    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.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    9

    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?

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    9

    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

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    9

    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."

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Matlab function in VB.NET does not work. What's wrong with it?

    Code:
    Dim result As Object = New Object()

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    9

    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)".

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Matlab function in VB.NET does not work. What's wrong with it?

    Can you post the code you're using.

  11. #11

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    9

    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

  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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?

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    9

    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"

  15. #15

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    9

    Re: Matlab function in VB.NET does not work. What's wrong with it?

    Quote Originally Posted by techgnome View Post
    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.

  16. #16

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    9

    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
  •  



Click Here to Expand Forum to Full Width