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: Copying structures


From   Matthew Baker <[email protected]>
To   [email protected]
Subject   Re: st: Copying structures
Date   Mon, 15 Apr 2013 09:26:34 -0400

Matt --

That is mysterious indeed! I found that if I modify your example code
to just include an additional step in the mysubroutine function,
things seem to go okay and the original vector structure isn't
overwritten. All I do is add in a dummy structure, which is just set
equal to mypoints at the beginning of the program, and then the
program runs using values of the standin structure. Now, the orginal
row vector isn't overwritten. While this evidently works, it isn't an
explanation!

Here is my modification of your simple example:

clear all
mata:
struct point {
    real scalar x, y
}
void function myfunc()
{
    struct point rowvector mypoints

    mypoints = point(2)
    mypoints[1].x = mypoints[1].y = 1
    mypoints[2].x = mypoints[2].y = 2

    liststruct(mypoints)
    mysubroutine(mypoints)
    liststruct(mypoints)
}
void function mysubroutine(struct point rowvector mypoints)
{
struct point scalar firstpoint
struct point rowvector standin
standin=mypoints
firstpoint=standin[1]
firstpoint.x=firstpoint.x+1
}
myfunc()

end

Best,

Matt














On Mon, Apr 15, 2013 at 12:26 AM, Matthew White
<[email protected]> wrote:
> Hi Statalist members,
>
> I'm using a Mata structure in a function that looks like this:
>
> void function write_lists(struct odklist rowvector lists)
> {
>     real scalar i
>     struct odklist scalar list
>     struct odklist rowvector newlists
>
>     newlists = odklist()
>     for (i = 1; i <= length(lists); i++) {
>         list = lists[i]
>
>         // Modify variable list.
>
>         // If list satisfies certain criteria, add it to newlists.
>     }
>
>     // More code
>
>     for (i = 1; i <= length(newlists); i++) {
>         // Do stuff with newlists[i].
>     }
> }
>
> My problem is that over the course of the function, the argument lists
> is modified: after each element of lists is copied to list (-list =
> lists[i]-), the element is modified whenever list is modified. It's as
> if list and lists[i] have been linked.
>
> Here's a simpler example:
>
> mata:
>
> struct point {
>     real scalar x, y
> }
>
> void function myfunc()
> {
>     struct point rowvector mypoints
>
>     mypoints = point(2)
>     mypoints[1].x = mypoints[1].y = 1
>     mypoints[2].x = mypoints[2].y = 2
>
>     liststruct(mypoints)
>     mysubroutine(mypoints)
>     liststruct(mypoints)
> }
>
> void function mysubroutine(struct point rowvector mypoints)
> {
>     struct point scalar firstpoint
>
>     firstpoint = mypoints[1]
>     firstpoint.x = firstpoint.x + 1
> }
>
> myfunc()
>
> end
>
> Before -mysubroutine()-, mypoints[1].x = 1. Afterwards, mypoints[1].x
> = 2. This is despite the fact that the only use of mypoints is to make
> firstpoint a copy of mypoints[1].
>
> It seems to work fine to copy a struct scalar to another struct scalar
> or a struct vector to another struct vector, but copying an element of
> a struct vector to a struct scalar results in this behavior.
>
> I'm using Stata 12.1 SE on Windows 7. Any suggestions would be much appreciated.
>
> Thanks,
> Matt
>
> --
> Matthew White
> Senior Project Associate
> Innovations for Poverty Action
> 101 Whitney Avenue, New Haven, CT 06510 USA
> www.poverty-action.org
> *
> *   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/



-- 
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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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