Announcement

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

  • mata structure to return function results - error

    This is probably a basic question, but i cant figure out from tutorials my where is my error. I have a function that should return 2 objects, a matrix and a vector. I tried to use structure for it, in this way:
    structure mystr {
    real matrix points
    real colvector weights
    }

    structure mystr scalar myfcn(real scalar n,
    | real scalar pr)
    {
    structure strres scalar mgh

    ... i compute matrix(called points) and vector(called weights)

    mgh.points = points
    mgh.weights = weights
    return(mgh)
    }

    When i run it i get error at the very beginning:
    : structure mystr {
    invalid expression
    (153 lines skipped)

    What am i doing wrong? Thanks for help
    Last edited by Gbenga Adejafe; 17 Apr 2016, 18:53.

  • #2
    See following example, notice Mata structure is defined using keyword struct instead of structure

    Code:
    cscript
    
    mata:
    
    struct mystr {
        real matrix points
        real colvector weights
    }
    
    struct mystr scalar myfcn()
    {
        struct mystr scalar a
        
        a.points = J(2, 2, 1)
        a.weights = J(2, 1, 3)
        return(a)
    }
    
    void test()
    {
        struct mystr scalar r
        
        r = myfcn()
        r.points
        r.weights
    }
    
    test()
    end

    Comment


    • #3
      Thank you a lot, it works in this way

      Comment

      Working...
      X