 Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: How to return a Mata matrix in Stata

 From Alex Olssen To statalist@hsphsun2.harvard.edu Subject st: How to return a Mata matrix in Stata Date Fri, 12 Aug 2011 21:51:55 +1200

```Dear Statalisters,

Can anyone help me return some matrices in a Stata program that calls
Mata?  My plan was to use -st_mata()- to names for the matrices and
then try a -return matrix- call in Stata.  Any help is appreciated.
Hopefully once I get my head around Mata I will be able to help some
people. :)  My code is below.  You can paste it straight into the
do-file editor and run it.  The problems are in the two lines that are
commented out.

************************************************************
sysuse auto, clear
keep price length weight

capture program drop mytheta
program define mytheta, rclass
version 11
mata theta()
display as txt " theta = " as res r(theta)
// return matrix eigval = r(eigval)
// return matrix eigvec = r(eigvec)
return scalar theta = r(theta)
end

capture mata mata drop theta()
mata
void theta()
{
st_view(test = ., ., .)
test_cov = variance(test)
st_matrix("r(v)", test_cov)
test_eigen = symeigensystem(test_cov, X = ., L = .)
st_matrix("r(eigval)", X)
st_matrix("r(eigvec)", L)
st_numscalar("r(theta)", L/sum(L))
}
end

mytheta
************************************************************

Kind regards,
Alex Olssen
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```