Announcement

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

  • Behavior of mata matsave in programs

    The mata matsave help file
    Code:
    help mata_matsave
    warns
    These commands are for interactive use; they are not for use inside programs.
    With that in mind can anyone explain the behavior of mata matsave in the following contexts. I'm particularly interested in why there's no error in the first and second cases but an error in the third case.
    Code:
    mata
     z=J(10,10,1)
     mata matsave ztestmat1 z, replace
    end
    
    mata
    k=1
    if (k==1) {
     z=J(10,10,1)
    }
    stata("mata: mata matsave ztestmat2 z, replace")
    end
    
    mata
    k=1
    if (k==1) {
     z=J(10,10,1)
    }
    mata matsave ztestmat3 z, replace
    end
    Results:
    Code:
    . mata
    ------------------------------------------------- mata (type end to exit) ---------------------------
    :  z=J(10,10,1)
    
    :  mata matsave ztestmat1 z, replace
    (saving z[10,10])
    file ztestmat1.mmat saved
    
    : end
    -----------------------------------------------------------------------------------------------------
    
    .
    . mata
    ------------------------------------------------- mata (type end to exit) ---------------------------
    : k=1
    
    : if (k==1) {
    >  z=J(10,10,1)
    > }
    > stata("mata: mata matsave ztestmat2 z, replace")
    (saving z[10,10])
    file ztestmat2.mmat saved
    
    : end
    -----------------------------------------------------------------------------------------------------
    
    .
    . mata
    ------------------------------------------------- mata (type end to exit) ---------------------------
    : k=1
    
    : if (k==1) {
    >  z=J(10,10,1)
    > }
    > mata matsave ztestmat3 z, replace
    invalid expression
    r(3000);
    
    : end
    -----------------------------------------------------------------------------------------------------
    
    .  
    .
    end of do-file

  • #2
    I might add the following fourth case, which differs from the third only by the placement of a semicolon on the line after the close-bracket:
    Code:
    mata
    k=1
    if (k==1) {
     z=J(10,10,1)
    }
    ;
    mata matsave ztestmat4 z, replace
    end
    Results:
    Code:
    . mata
    ------------------------------------------------- mata (type end to exit) ---------------------------
    : k=1
    
    : if (k==1) {
    >  z=J(10,10,1)
    > }
    > ;
    
    : mata matsave ztestmat4 z, replace
    (saving z[10,10])
    file ztestmat4.mmat saved
    
    : end

    Comment


    • #3
      In light of the fact that the
      Code:
      stata(...)
      command in #1 worked without error I wrote the following functions that may be useful for executing the equivalent of mata matsave and mata matuse commands inside a Mata program.
      Code:
      /******************************************************************************/
      /*                                                                            */
      /* savemat: function to execute the equivalent of a -mata matsave- command    */
      /*     inside a Mata program.                                                 */
      /*                                                                            */
      /* usemat: function to execute the equivalent of a -mata matuse- command      */
      /*     inside a Mata program.                                                 */
      /*                                                                            */
      /* (See "Remarks" under -help mata_matsave- for context)                      */
      /*                                                                            */
      /* Syntax:  savemat("existingmatamatrixname","matrixnametosave",r)            */
      /*          usemat("savedmatrixname",r)                                       */
      /*                                                                            */
      /*   existingmatamatrixname is a 1x1 string containing the name of the        */
      /*      existing Mata matrix to be saved                                      */
      /*                                                                            */
      /*   matrixnametosave is a 1x1 string containing the name of the file to be   */
      /*      saved (will be suffixed with .mmat unless specified otherwise)        */
      /*                                                                            */
      /*   savedmatrixname is a 1x1 string containing the name of the file that     */
      /*      contains the saved matrix (.mmat suffix assumed unless specified      */
      /*      otherwise)                                                            */
      /*                                                                            */
      /*   r is a real scalar, r=0 to not replace, r=1 to replace                   */
      /*                                                                            */
      /* Example: savemat("xymat","xymatsaved",0)                                   */
      /*                                                                            */
      /******************************************************************************/
      
      void savemat(mat,name,r) {
       string s1
       if (r==0) {
        s1="mata: mata matsave "+name+" "+mat
        stata(s1)
       }
       if (r==1) {
        s1="mata: mata matsave "+name+" "+mat+",replace"
        stata(s1)
       }
      }
      
      void usemat(name,r) {
       string s1
       if (r==0) {
        s1="mata: mata matuse "+name
        stata(s1)
       }
       if (r==1) {
        s1="mata: mata matuse "+name+",replace"
        stata(s1)
       }
      }

      Comment

      Working...
      X