Statalist The Stata Listserver


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

st: Re: Saving to locals in Mata


From   "Julian Reif" <[email protected]>
To   <[email protected]>
Subject   st: Re: Saving to locals in Mata
Date   Thu, 26 Apr 2007 10:56:30 -0400

Alan,

Thanks for your response. That resolved my issue.

The reason I am creating the locals in the first place is because they are serving as arguments for a Stata plugin that was written in C. I can't port the plugin to Mata because the plugin does things Mata cannot do (or that I don't know how to do in Mata, at least). Is there a way to directly access plugins via Mata instead of via the Stata interface?

Julian

-----Original Message-----
From: [email protected]
[mailto:[email protected]]
Sent: Thursday, April 26, 2007 2:33 AM
To: [email protected]
Subject: statalist-digest V4 #2678


Julian Reif ([email protected]) asked about saving locals
from Mata to Stata:
> I am having trouble saving to locals using Mata. The following ADO code demonstrates my problem:
> 
> ***example.ado code***
> 
> 	program example
> 		version 9.2
> 
> 		mata: example()
> 	end
> 
> 	mata:
> 		void example()
> 		{
> 			t = "string"
> 			st_local("my_local",t)
> 			stata(`" di "my_local is `my_local'""')
> 		}	
> 	end
> 
> ***end example.ado code***
> 
> my_local is displayed as blank instead of "string". However, if I run the three lines of mata code inside of example() interactively, my_local has the contents "string". Why does this work interactively but not in my program? How should this have been coded so that it works?
> 
> The same problem happens if I use globals instead of locals.

Kit Baum and Maarten Buis suggested looking at the value of the
local after the Mata function example() was through.

The local actually exists in Stata immediately after Mata created
it with

   ...
   t = "string"
   st_local("my_local",t)
   ...

Julian could prove this by adding the line

   stata("macro list")

immediately after the call to st_local().  So, if my_local has been
created when the Mata function example() runs, why doesn't the
call to -display- from the stata() function work?

The reason Julian is not able to 'see' it in Stata with this line

   stata(`" di "my_local is `my_local'""')

is because what gets passed to Stata to be executed is not what
he thinks.

Remember that Mata is a compiled language, and for it to be
compiled, it must be first read by Stata.  When Stata reads a line
of Mata code, Stata AT READ TIME applies its normal macro substitution
rules.  When the line

   stata(`" di "my_local is `my_local'""')

was initially read by Stata (while it was acquiring lines of the
example() function to be compiled), my_local did not exist.  Thus,
what was actually seen to be compiled was

   stata(`" di "my_local is ""')

When that line is later executed, of course all that is output by Mata
is

   my_local is 

Julian needs to make sure that the literal string

   di "my_local is `my_local'"

is seen by Stata when it is reading and compiling the Mata code.  He
can do this by protecting `my_local' from macro expansion with a
backslash:

   stata(`" di "my_local is \`my_local'""')


Then, at read/compile time, Stata won't try to expand the non-existent
macro my_local and the literal string

   di "my_local is `my_local'"

will be stored in the compiled Mata function example() to be passed
to the stata() function for execution later.


Alan
([email protected])

*
*   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