Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Question regarding formatting in Sprintf command (Mata)


From   "Thomas Jacobs" <[email protected]>
To   StataList <[email protected]>
Subject   st: Question regarding formatting in Sprintf command (Mata)
Date   Tue, 16 Oct 2007 12:54:28 -0500

I am using the following routine to "export" a Mata matrix to Stata by
writing a text file which I then insheet into Stata and am confused
about why the sprintf command does not need to end with a \n newline
character:

//Write Results matrix to file
fh = fopen("Results.txt", "w")

for (j=1; j<=rows(Results); j++) {

	fput(fh,sprintf("%22.0g\t%22.0g\t%22.0g\t%22.0g\t%22.0g\t%22.0g",
			Results[j,1],Results[j,2],Results[j,3], Results[j,4],
Results[j,5],Results[j,6]))

}

fclose(fh)

If I add the \n newline character I get my output file, Results.txt
with interspersed blank rows between each row of data.

I made the formatting %22.0g from %12.0g to see if the automatic
newline was caused by exceeding some predefined page width.  No matter
how I play with the field lengths I only get the appropriate file in
notepad or wordpad (under MS Windows) if I exclude the \n character.
I don't understand this behavior.  Can someone offer an explanation?
Thanks.

Sincerely,

Tom

-- 
Thomas Jacobs
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index