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]

Re: st: Avoiding use of Mata external variables in large programs


From   Christophe Kolodziejczyk <[email protected]>
To   [email protected]
Subject   Re: st: Avoiding use of Mata external variables in large programs
Date   Wed, 8 May 2013 15:46:44 +0200

Dear Michael
I would use structures or classes to avoid external. Structures cannot
be used interactively though.
Best
Christophe



2013/5/7 Lacy,Michael <[email protected]>:
> I'm seeking advice about avoiding the use of Mata external variables in
> Mata/Stata adofile programs.  This use arises in passing intermediate
> results among different Mata functions, as I illustrate below, but there
> are occasions where this might arise. My concerns have to do with
> keeping Mata variables and their names as limited in scope as possible,
> to avoid me or someone else accidentally clobbering them.
>
> My general approach in using Mata is what I presume as typical, i.e. to  use a
> Stata program as the main program, and call Mata functions within it.  I find
> it convenient to use Mata external variables to retain Mata results until
> the next Mata function needs them.  Schematically, we might have something like:
>
>
> prog MyStataProg
>    <some Stata commands>
>   mata: E1 = MyMata1(...)
>    <some more Stata stuff>
>    mata: MyMata2(...)  // which uses E1 and get access to it by declaring E1 as external
> end
>
> I think of having E1 visible at the "main" level Mata as undesirable.  Further, My E1 could
> step on a variable name used in Mata by me or someone else.
>
> My thought for avoiding this are:
>
> 1) Use Stata's -tempname- to declare E1
> tempname E1
> mata: `E1' = ...
>
> 2) Make a Mata function as the main program, so that E1 is within it:
> mata:
> MyMainMataProgram() {
>   stata(cmd1)
>   E1 = MyMata1()
>  stata(cmd2)
>   MyMata2(E1, ...)
> }
> =========================
>
> Both options 1) and 2)  seem unattractive.  Can others comment on these options,
> including using "unhidden" externals as I suggested in my schematic, and/or suggest
> something else?  I understand that I could retain some intermediate results like E1
> as Stata scalars or matrices, but that is often clumsy or impractical.
>
> Regards,
>
> Mike Lacy
> Dept. of Sociology
> Colorado State University
> Fort Collins CO 80523-1784
>
>
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/



-- 
Christophe Kolodziejczyk
Research Fellow

AKF, Anvendt KommunalForskning
Danish Institute of Governmental Research
Købmagergade 22
DK-1150 København K

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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