Announcement

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

  • Question about mata commands that start with -mata-

    Hi – I wrote a mata function called -pr_exists()- that tests if a mata object exists. See below. My intent was to include it in programs to test if something exists before issuing a -mata drop _object_ - command, but it turns out that -mata _stuff_ - commands do not work inside if{} or do{} blocks of code. I understand why. But, how can I solve this problem? Thx! -- P

    . mata
    ------------------------------------------------- mata (type end to exit) -----
    :
    : void function saySomething(string scalar x)
    > {
    > printf("%s",x)
    > return
    > }

    : saySomething("I said this")
    I said this
    :
    :
    : real scalar function pr_exists(string scalar x)
    > {
    > /* tests and returns 1/0 if mata object exists */
    > return(findexternal(x)!=NULL)
    > }

    : pr_exists("x")
    0

    : pr_exists("func()")
    0

    : pr_exists("saySomething()")
    1

    :
    : if (pr_exists("saySomething()")) x=6
    > x
    6

    :
    : if (pr_exists("saySomething()")) mata drop saySomething()
    invalid expression
    r(3000);

    :
    : end
    -------------------------------------------------------------------------------

    . exit



  • #2
    Paul: Try substituting this (note the semi-colon on the 2nd line):
    Code:
     if (pr_exists("saySomething()")) stata("mata: mata drop saySomething()")
     ;
    Essentially you temporarily leave Mata to go into Stata to tell Mata what to do.

    In my experience Mata's behavior can be "curious" in cases where the help files indicate
    This command is for use in Mata mode following Mata's colon prompt.
    And understanding why semi-colons sometimes are helpful is above my paygrade.

    Comment


    • #3
      This worked, John. I did not use the semi-colon. I did not need that. Rather, I just needed to be schooled on the mata function stata(). Hope you are well. -- P

      Comment


      • #4
        Glad it worked, Paul. The semi-colon issue is baffling to me. When I exclude it the code fails.

        Here's my modification of your code without the semi-colon
        Code:
        cap mata mata drop saySomething()
        cap mata mata drop pr_exists()
        
        mata
        
        void function saySomething(string scalar x)
        {
            printf("%s",x)
            return
        }
        
        saySomething("I said this")
        
        real scalar function pr_exists(string scalar x)
        {
            /* tests and returns 1/0 if mata object exists */
            return(findexternal(x)!=NULL)
        }
        
        pr_exists("x")
        
        pr_exists("func()")
        
        pr_exists("saySomething()")
        
        
        if (pr_exists("saySomething()")) x=6
        x
        
        if (pr_exists("saySomething()")) stata("mata: mata drop saySomething()")
        
        end
        The last few lines of the output are:
        Code:
        : pr_exists("saySomething()")
          1
        
        :
        :
        : if (pr_exists("saySomething()")) x=6
        > x
          6
        
        :
        : if (pr_exists("saySomething()")) stata("mata: mata drop saySomething()")
        >
        > end
        
        unexpected end of line
        -----------------------------------------------------------------------------------------------------
        r(3000);
        
        end of do-file
        
        r(3000);
        
        .
        When I include the semi-colon as here
        Code:
        <code omitted>
        if (pr_exists("saySomething()")) x=6
        x
        
        if (pr_exists("saySomething()")) stata("mata: mata drop saySomething()")
        ;
        
        end
        everything runs fine, i.e.
        Code:
        : pr_exists("saySomething()")
          1
        
        :
        :
        : if (pr_exists("saySomething()")) x=6
        > x
          6
        
        :
        : if (pr_exists("saySomething()")) stata("mata: mata drop saySomething()")
        > ;
        
        :
        : end
        -----------------------------------------------------------------------------------------------------
        
        .  
        .
        end of do-file
        
        .
        I'd be grateful for any insights the Mata cognoscenti may be able to offer.
        Last edited by John Mullahy; 05 Dec 2023, 06:39.

        Comment

        Working...
        X