Announcement

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

  • Mata: drop all matrices, but keep the functions

    Hello,

    I am looking for a procedure that drops all matrices in mata, but keeps the functions that I have defined so far. "mata clear" does not work because it also drops the functions. I have thought about using "mata describe" and then looping over all matrices, but I don't know how to construct such a loop (e.g. I don't know how to recover the list of matrices and loop over the list). The following example will clarify what I want:

    Code:
    version 13
    mata
        mata clear
        string scalar prstr(string scalar strtopr) {
            return(2*strtopr) 
        }
        x=("a","b")
        y=("c","d")'
        mata describe
        x
        prstr(x[1,1])
        // Procedure I am looking for
        // Kind of a "drop if matrix"
        mata describe // This should only return prstr()    
        
    end

  • #2
    All the answers in this thread were deleted somehow. For future reference, this is the function that I wrote with the help of other members of Statalist:

    Code:
    version 13
    clear all
    
    mata
        void dropmats() {
    
            /*        
            This function drops all the external matrices from mata, without dropping mata functions        
            */
    
            string vector mataobj
            real scalar i  
         
            mataobj=direxternal("*")
            for (i=1; i<=rows(mataobj); i++) {
                rmexternal(mataobj[i])
            }        
    
        }
    
        x=("a","b")
        y=("c","d")'
        mata describe    
        dropmats()
        mata describe
    
    end

    Comment


    • #3
      Carlos --

      Very nice and thanks for posting. I'm curious if it is possible to do the opposite - that is, drop all functions without dropping matrices. This might be useful when one is trying to write/test/debug a bunch of functions at once.

      Best,

      Matt Baker

      Comment


      • #4
        Code:
        : mata des
        
              # bytes   type                        name and extent
        -------------------------------------------------------------------------------
                   64   void                        f()
                    8   real scalar                 a
                   24   real rowvector              b[3]
        -------------------------------------------------------------------------------
        
        : mata drop *()
        
        : mata des
        
              # bytes   type                        name and extent
        -------------------------------------------------------------------------------
                    8   real scalar                 a
                   24   real rowvector              b[3]
        -------------------------------------------------------------------------------
        Best, Sergiy Radyakin

        Comment


        • #5
          Hi
          Actually looking at the namelist definition for mata -help m3_namelists- it says
          ...hence,
          * means all matrices
          *() means all functions
          * *() means all matrices and all functions
          So:
          Code:
          . mata
          :         mata clear
          : 
          :     function spam()
          >         {
          >                 return("Absolutely nothing")
          >     }        
          : 
          :         a = 5
          :     x=("a","b")
          :     y=("c","d")'
          :     mata describe
                # bytes   type                        name and extent
          -------------------------------------------------------------------------------
                     64   transmorphic matrix         spam()
                      8   real scalar                 a
                     18   string rowvector            x[2]
                     18   string colvector            y[2]
          -------------------------------------------------------------------------------
          
          :         // You can drop all variables simply by (This is easier than the dropmat function above)
          :     mata drop *
          
          :     mata describe
                # bytes   type                        name and extent
          -------------------------------------------------------------------------------
                     64   transmorphic matrix         spam()
          -------------------------------------------------------------------------------
          : 
          :         // Variables are added again
          :         a = 5
          :     x=("a","b")
          :     y=("c","d")'
          :     mata describe
                # bytes   type                        name and extent
          -------------------------------------------------------------------------------
                     64   transmorphic matrix         spam()
                      8   real scalar                 a
                     18   string rowvector            x[2]
                     18   string colvector            y[2]
          -------------------------------------------------------------------------------
          
          :         // You can drop all functions simply by (Just as Sergiy says)
          :         mata drop *()
          
          :         mata describe
                # bytes   type                        name and extent
          -------------------------------------------------------------------------------
                      8   real scalar                 a
                     18   string rowvector            x[2]
                     18   string colvector            y[2]
          -------------------------------------------------------------------------------
          : end
          Kind regards

          nhb

          Comment


          • #6
            Hi again
            My code will only work sometimes due to errors, see http://www.statalist.org/forums/foru...rtual-function and http://www.statalist.org/forums/foru...assdef-_b_stat.

            The code work in the above example, but in a another case I just I all of a sudden had problems with _b_stat as described in the links when I wanted to delete functions.
            Kind regards

            nhb

            Comment

            Working...
            X