Announcement

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

  • Using -syntax- to get Mata matrix definitions and evaluating these in Mata

    Hi
    I would like to build a Stata command retrieving a string definition of a Mata matrix, evaluate it and send the matrix into some mata code.
    So I started with numbers and all went well:
    Code:
    . clear
    . mata mata clear
    . local 0 , code(1, 2 \ 3, 4)     // Setting up input to test syntax
    . syntax, code(string)
    . capture mata M = `code'         // Evaluating mata matrix
    . if _rc mata M = J(0,0,.)        // Default value if error evaluating
    . mata M
           1   2
        +---------+
      1 |  1   2  |
      2 |  3   4  |
        +---------+
    Nice!! An easy way to pass matrices from Stata to Mata.

    Now I need to do this for both real and string matrices, so I went on:
    Code:
    . clear
    . mata mata clear
    . local 0 , code("l", "r" \ "c", "r")     // Setting up input to test syntax
    . syntax, code(string asis)
    . capture noisily mata M = `code'         // Evaluating mata matrix
    . if _rc mata M = J(0,0,"")               // Default value if error evaluating
    . mata M
           1   2
        +---------+
      1 |  l   r  |
      2 |  c   r  |
        +---------+
    The key point is to use asis defining string input in the -syntax- command when evaluating string matrices.
    Using asis when evaluating real matrices doesn't change a thing, so one could choose to use asis always in cases like this.


    Similar things has been discussed previously in http://www.stata.com/statalist/archi.../msg00775.html and in http://www.stata.com/statalist/archi.../msg00925.html


    Hopefully someone finds this usefull
    Kind regards

    nhb
Working...
X