Hi,
I am now coding programs using Stata and Mata simultaneously, so I have to pass information between Mata and Stata. I found out that there are more than one ways to pass things between these two environments and I am kind of curious which approach is more efficient.
From Stata to Mata:
Approach 1: Use st_matrix. And by using this approach, you need to pass the matrix's name to the Mata function. Currently, I am using this way.
Approach 2: Pass a numeric matrix to the Mata function. I have tried this way, but it seems like it doesn't work well. Stata tells me she could not find the matrix. But in Stata manual, I have learned this approach. I don't know where went wrong. Followed is my code.
capture program drop tt9
program tt9,rclass
mat A = (2,2,2,2,2)
mata: test9(A)
return mat m = r(m)
end
capture mata mata drop test9()
mata:
void test9(numeric matrix A)
{
numeric matrix B, C
B = (2,3,4,5,6)
C = A + B
st_matrix("r(m)",C)
}
end
From Mata to Stata:
Approach 1: Use st_matrix. And by using this approach, you don't need to return anything in Mata function. Currently, I am now using this way.
Approach 2: Use return in Mata function.
I am curious which approach is better or they just work equally well. What's your opinions? Thank you very much for finishing reading these!
I am now coding programs using Stata and Mata simultaneously, so I have to pass information between Mata and Stata. I found out that there are more than one ways to pass things between these two environments and I am kind of curious which approach is more efficient.
From Stata to Mata:
Approach 1: Use st_matrix. And by using this approach, you need to pass the matrix's name to the Mata function. Currently, I am using this way.
Approach 2: Pass a numeric matrix to the Mata function. I have tried this way, but it seems like it doesn't work well. Stata tells me she could not find the matrix. But in Stata manual, I have learned this approach. I don't know where went wrong. Followed is my code.
capture program drop tt9
program tt9,rclass
mat A = (2,2,2,2,2)
mata: test9(A)
return mat m = r(m)
end
capture mata mata drop test9()
mata:
void test9(numeric matrix A)
{
numeric matrix B, C
B = (2,3,4,5,6)
C = A + B
st_matrix("r(m)",C)
}
end
From Mata to Stata:
Approach 1: Use st_matrix. And by using this approach, you don't need to return anything in Mata function. Currently, I am now using this way.
Approach 2: Use return in Mata function.
I am curious which approach is better or they just work equally well. What's your opinions? Thank you very much for finishing reading these!
Comment