Bookmark and Share

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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Generating intermediate output with a program that calls a mata function


From   Matthew Baker <[email protected]>
To   [email protected]
Subject   st: Generating intermediate output with a program that calls a mata function
Date   Wed, 18 Jul 2012 09:48:17 -0400

Dear Listers --

I am curious if there is a way for one to generate intermediate output
from within a mata function that is called by a program. To be
specific, I have a .ado file that passes data consisting of a number
of cases to a mata file, which then loops over the cases. What I would
like to do is to have the program generate some simple confirmation
that it is progressing through the cases as it goes - even something
as simple as a dot ".".

The problem is, the program calls mata, which then loops over the
cases, only printing out the "intermediate confirmation output" once
the program is finished. Perhaps an example is best - in the following
(trivial) example, the program calls a mata routine which loops over
cases, and I would like it to print something IMMEDIATELY after the
computation involving a case is done, not after the program is
finished running:

/* Begin example */

/* Create fictional data */
clear all
set obs 9
gen id=1
replace id=2 in 4/6
replace id=3 in 7/9
gen x=runiform()

/* A program which calls a mata looping function "dosomething" */

capture program drop simpexample
program simpexample
	syntax varlist, GROUP(varname) CREATE(string)
	gen `create'=.
	mata: st_view(id=.,.,"`group'")
	mata: st_view(x=.,.,"`varlist'")
	mata: st_view(z=.,.,"`create'")
	mata: z[.,.]=dosomething(x,id)
end

/* The mata function that loops over cases and tries to print
something when done with a case */

mata:
real matrix dosomething(x,id)
{
	real matrix Z,m
	real scalar i
	Z=J(rows(x),1,.)
	m=panelsetup(id,1)
	for (i=1;i<=rows(m);i++) {
		xp=panelsubmatrix(x,i,m)
		Z[m[i,1]::m[i,2]]=xp:^2
		printf("Case done")
							}
    return(Z)
}
end

simpexample x, group(id) create(y)

/* the program only prints "case done" once the program is done, not
as the mata function completes cases! */

/* End example */

Thanks in advance for the help!

Matt Baker

-- 
Dr. Matthew J. Baker
Department of Economics
Hunter College and the Graduate Center, CUNY
*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index