Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • The pointer does not work in a very simply situation

    I am experiencing a really confusing situation.
    My MATA code is very short like below:
    Code:
    mata: pointer(real matrix) colvector mypoint
    It is hard to think that the code returns an error because the code is extremely simple.
    But, that returns an error "nothing found where '(' expected"

    I cannot come up with the cause and solution.

  • #2
    Code:
    pointer(real matrix) colvector mypoint
    Is a declaration. Declarations can only take place inside a function. So:

    Code:
    mata:
    void myfunc()
    {
        pointer(real matrix) colvector mypoint
    }
    myfunc()
    end
    will work, but
    Code:
    mata:
        pointer(real matrix) colvector mypoint
    end
    will return an error. I agree that the error message is confusing.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Originally posted by Minchul Park View Post
      I cannot come up with the cause and solution.
      In interactive mode, type and organization are determined when you assign something to the variable.

      In order to avoid that error message, you'll need to declare the variable in the context of a function definition, which you can do at the command line, for example, you can type all of the following at the command line instead of in a do-file.
      Code:
      version 18.0
      
      mata:
      mata clear
      
      pointer(real matrix) colvector function test(real matrix A, real matrix B) return((&A \ &B))
      
      Ptr = test((1, 2), (3, 4))
      eltype(Ptr)
      orgtype(Ptr)
      
      mata drop test()
      
      void function test(real matrix A, real matrix B, pointer(real matrix) colvector P) P = (&A \ &B)
      
      test((1, 2), (3, 4), Ptr)
      eltype(Ptr)
      orgtype(Ptr)
      
      end
      
      exit // <- don't type this at the command line
      You can also do variable declaration inside the body of a function or struct, as well as declare member variables in class definitions.

      Comment


      • #4
        Maarten Buis Joseph Coveney Thank you for the reply. Due to my lack of knowledge about Mata programming, I didn't know that declaration only works inside a function. Thank you.

        Comment

        Working...
        X