Announcement

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

  • st_local does not work inside a function

    Hi
    It seems like st_local does not work inside a function as the example below shows:
    Code:
    . mata:
    :         mata clear
    :         txt = "st_local works outside"
    :         st_local("local_outside", txt)
    :         
    :         function test(txt)
    >         {
    >                 txt = "st_local do not work inside"
    >                 st_local("local_inside", txt)
    >         }
    :         st_dir("local", "macro", "*")
      local_outside
    : end
    . display "`local_outside'|`local_inside'"
    st_local works outside|
    Or am I missing something? (Seen before)
    Kind regards

    nhb

  • #2
    Well, I guess you need to call your function test at least once. What you show here merely defines the function.

    Code:
    . mata:
    ------------------------------------------------- mata (type end to exit) --------------------------------------------------------------------------------------------------------------------------
    : mata clear         
    
    : txt = "st_local works outside"
    
    : st_local("local_outside", txt)
    
    : function test(txt)
    > {
    > txt = "st_local do not work inside"
    > st_local("local_inside", txt)
    > }
    
    : st_dir("local", "macro", "*")
      local_outside
    
    : 
    : test(.)
    
    : end
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    . display "`local_outside'|`local_inside'"
    st_local works outside|st_local do not work inside
    Best
    Daniel
    Last edited by daniel klein; 18 Mar 2016, 04:25.

    Comment


    • #3
      Hi Daniel
      Ah, I should of course call the function
      And your code runs fine by me as well.
      I was trying to make a simple example, not very successfully though.

      But I still have the problem, and to be more specific it occurs when the value is passed as an argument:
      Code:
      . mata:
      :         mata clear
      :         txt = "st_local works outside"
      :         st_local("local_outside", txt)
      :         
      :         function test(string scalar txt)
      >         {
      >                 st_global("local_inside", txt)
      >         }
      : 
      :         test("st_local do not work inside")
      :         st_dir("local", "macro", "*")
        local_outside
      : end
      . display "`local_outside'|`local_inside'"
      st_local works outside|
      Kind regards

      nhb

      Comment


      • #4
        Now you are using st_global() inside the Mata function but referencing a local in Stata ...

        Comment


        • #5
          Hi Daniel
          Sorry for taking your time.
          Now it works with st_local.

          I can't explain what happened here.
          Luckily it will be weekend soon.

          Thank you very much for your help.
          And have a nice weekend.
          Kind regards

          nhb

          Comment


          • #6
            No need for apologies, I can relate. Do not forget to insert a comment in the code, reminding you to check the parts your are writing now again later, when you are refreshed and concentrated.

            Comment


            • #7
              will do
              Kind regards

              nhb

              Comment

              Working...
              X