Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • mata equivalent of Stata's preserve?

    Dear Matalisters,

    Is there an equivalent of Stata's preserve for Mata?

    I am in a situation where it would be convenient to take a snapshot of all the objects in Mata's memory, and later restore Mata to this state.

    Thanks,

    Bert


  • #2
    Originally posted by Bert Lloyd View Post
    Is there an equivalent of Stata's preserve for Mata?
    Not to my knowledge.

    I am in a situation where it would be convenient to take a snapshot of all the objects in Mata's memory, and later restore Mata to this state.
    You could try creating a pointer to each object and parking the pointers in an associative array for the time being, and then retrieving and dereferencing them all afterward.

    You could also park functions and matrixes in a Mata library; here you could use a wildcard to refer to them all at once, but I'm not sure that this approach will preserve state (contents) of the matrixes.

    It's a curious situation to be in. If you anticipate it again, you might want to consider working with Mata's object-oriented programming features, designing classes that can conveniently contain the pertinent scalars and matrixes (and their current states), functions etc. in a collection of instantiated objects that you can keep available in memory while you do something else and then return attention to them when ready. I don't think that you can serialze them to disc, though.

    Comment


    • #3
      You can save all existing global matrixes using mata matsave, e.g.
      Code:
      mata
      mata matsave mywork *
      and then reload them later using mata matuse.

      See
      Code:
      help mata matsave
      This will not save user-defined functions or other objects, however.

      Comment


      • #4
        Thanks Joseph Coveney and John Mullahy for your suggestions.

        I should have mentioned that at least in this application I only need the matrices, not functions or other objects.

        Originally posted by Joseph Coveney View Post
        It's a curious situation to be in.
        Agreed.

        John Mullahy I had considered mata matsave but was concerned about the following in the helpfile:

        These commands are for interactive use; they are not for use inside programs. See [M-5] fopen() for Mata's programming functions for reading and writing files.


        I had considered something like the following:

        1. create a "storage" file using fopen

        Code:
        fh = fopen("mySnapshot.myfile", "w")
        2. List the contents of Mata's memory using
        mata describe.

        Code:
        mata describe
        3a. Access the list somehow

        Code:
        // don't know this part
        3.b Save each element of the list to "mySnapshot.myfile" along the lines of

        Code:
        fputmatrix(fh, X1)
        fputmatrix(fh, X1)


        possibly looping through the list, possibly something fancier

        3.c close the snapshot file

        Code:
        fclose(fh)
        4. [Do some other stuff until it's time to retrieve the objects]


        5.a Open the snapshot file for reading

        Code:
        fh = fopen("mySnapshot.myfile", "r")
        5.b retrieve each element

        Code:
        X1 = fgetmatrix(fh)
        X2 = fgetmatrix(fh)


        Is this a sensible thing to do?

        If so, I'd appreciate some advice on implementing it, especially 3a and 3b.

        Comment


        • #5
          In my experience the Mata warnings about interactive use only
          These commands are for interactive use; they are not for use inside programs. See [M-5] fopen() for Mata's programming functions for reading and writing files.
          are sometimes not binding.

          Consider this program
          Code:
          cap prog drop testmata
          prog def testmata
          
          mata: z=J(1,1,1)
          mata: mata matsave zmat z, replace
          
          end
          Results:
          Code:
          . testmata
          (saving z)
          file zmat.mmat saved

          Comment


          • #6
            Thanks I will give that a try.

            Comment

            Working...
            X