Announcement

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

  • How to pass a matrix to a Stata program??

    Hi,

    I am wondering whether there are any ways to pass a matrix to a Stata program.

    I think the only way is to use option in syntax. But it seems like that option only accepts real scalar and string. I have found a suggestion in the following link (http://www.stata.com/statalist/archi.../msg00311.html). The author suggests that maybe we can use the option and pass the name of the matrix, which is a string, to Stata program. However, I have try that but it seems like that it doesn't work. What follows is my code. I am not sure whether I have made some mistakes or this method doesn't work, since I am a new learner of Stata. Right now I can just define those matrices inside the Stata program, which would make it inconvenient for users to change the contents of the matrices outside the program. Any better idea? Thank you so much!

    capture program drop tt8
    program tt8,rclass
    version 13
    syntax , Matt(string)
    mat A=(1,2,3,4)
    mat B=A+`matt'
    return mat B
    end

  • #2
    Which part of the program does not work? You are advised by the FAQ to mention the type of error you get when you execute the program.

    Use
    Code:
    set trace on
    set tracedepth 1
    when you execute your program to see where it goes wrong.

    Modify the last line as
    Code:
    return mat B = B
    In your code I would also recommend to use temporary objects. If you happen to pass the matrix name A to your program, you will just multiply A = (1,2,3,4) by 2.

    Code:
    capture program drop tt8
    program tt8,rclass
    version 13
    syntax , Matt(string) tempname A B
    mat `A' = (1,2,3,4)
    mat `B' = `A'+`matt'
    return mat B = `B'
    end
    Now concerning your next post on sending Stata matrices to Mata and the use of st_matrix() with local names, you can use the function st_local().
    st_local() gives the (true) name of the object the local is referring to.
    In your code you can add the following lines to see what st_local() does.
    Code:
    mata: st_local("A"),st_local("B")
    mata: A = st_matrix(st_local("A"))

    Comment


    • #3
      Thank you so much!!! The code is working now. And thank you for the debugging method! It is useful!

      Comment

      Working...
      X