Announcement

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

  • Saving the output of a function in mata

    Hi,

    I am writing a program interacted with Mata. I do not know how to keep the output of Mata in the Mata environment. Let's have a look at the following simple multiplication program:

    Code:
    capture program drop matafunc
    program define matafunc
    syntax , VARiable(string)
    mata: mataf("`variable'")
    end
    mata
    function mataf(V) {
    st_view(Z,.,V)
    M = Z*2
    }
    end
    Simply run the matafunc program:
    Code:
    clear
    set obs 5
    gener double Z = runiform()
    matafunc, var(Z)
    M is the output of the program, which is two times Z (the input). This can be sent to Stata by adding st_matrix("M",M) to the end of the mataf function. How can I keep a copy of M in mata though?

    Thanks,
    Eilya.

  • #2
    Since M is created in a function, it will go out of scope (disappear) the moment the function ends. The easiest way to keep M is to let function mataf return it. The code looks as follows:
    Code:
    capture program drop matafunc
    program define matafunc
        syntax , VARiable(string)
        mata: M = mataf("`variable'")
    end
    
    mata
    real matrix function mataf(V) {
        st_view(Z,.,V)
        M = Z*2
        return(M)
    }
    end
    
    clear
    set obs 5
    gener double Z = runiform()
    matafunc, var(Z)
    mata: M // You now have a copy of M

    Comment


    • #3
      Hi Aljar,

      Thanks for your response. I wonder if there is anyway to save M but not to post it, i.e. I don't want to show the output (matrix M) on the screen. -return- does not save the matrix in mata.

      Regards,
      Eilya.
      Last edited by Eilya; 11 Sep 2014, 15:16.

      Comment


      • #4
        In Mata, the command -return(exp)- causes the function to stop execution and return to the caller, returning the evaluation of exp. In this case, the Mata function mataf() calls the return(M) so that the values in matrix M is returned to the caller of mataf(), namely the program matafunc. I rename the matrix that is defined in the program matafunc to A so that it wouldn't be confusing with the matrix M that is in the function mataf(). You may use this matrix A later.

        Code:
        capture program drop matafunc
        program define matafunc
            syntax , VARiable(string)
            mata: A = mataf("`variable'") // <--- rename the returned value to A, just to be clear
                                        //      A contains the values that were in M
                                        //      A can be used later on
        end
        
        mata
        real matrix function mataf(V) {
            st_view(Z,.,V)
            M = Z*2
            return(M) // <--- return the values of M to the caller, in this case matafunc
        }
        end  
        
        clear
        set obs 5
        gener double Z = runiform()
        matafunc, var(Z)
        //mata: A // You now have a copy of M // <--- this line just displays the matrix
                        // even if you don't display the matrix A, you can still use it
                        // you may use -mata: mata describe- to list the names of
                        // the matrices and functions in memory, including the amount of memory consumed by each.
        What would you want to do with A after you save it because this would change how to handle it in Mata? Thank you.
        Last edited by Fang Wang (StataCorp); 11 Sep 2014, 16:34.

        Comment

        Working...
        X