Announcement

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

  • Run mata code line-by-line from do-editor

    Sorry for the silly question, but let's say I have some mata code in the do-file editor that I would like to run line-by-line. Similarly to what I would do with R, for example (or with Stata code, for that matter). Is it possible to do this in Stata?

    Or do I have to enter mata from the command prompt, copy/paste each line of code from the do-file editor into the command prompt and run it from there? Maybe am I missing something?

  • #2
    What I've done is to start Mata via the Stata command window, and then copy and paste an individual line of Mata code onto the command line.
    Until prompted to try something by your message, I had not realized that "execute a selection" of Mata code from within the do-file editor causes an error.

    Comment


    • #3
      Andrea: It may be too clunky to be useful for your purposes, but in the do-file editor if you precede each line of Mata code with mata and then use the standard do-file operations it should work. E.g. Suppose the do-file editor contains:
      Code:
      mata x=uniform(1,1)
      mata x
      mata y=uniform(2,1)
      mata y
      mata z=uniform(3,1)
      mata z
      
      mata for (j=1;j<=3;j++) {
      j
      }
      If I highlight in the editor the 3rd through 6th lines and click Do, what is returned is:
      Code:
      . do "/var/folders/d5/6w9t_lzx3plbpmk6zpyz93dm0000gp/T//SD00791.000000"
      
      . mata y=uniform(2,1)
      
      . mata y
                       1
          +---------------+
        1 |  .6579563507  |
        2 |  .8215638404  |
          +---------------+
      
      . mata z=uniform(3,1)
      
      . mata z
                       1
          +---------------+
        1 |  .0060310856  |
        2 |  .5976252378  |
        3 |  .7201809312  |
          +---------------+
      
      .
      end of do-file
      If I highlight the 8th through 10th lines and and click Do, what is returned is:
      Code:
      . do "/var/folders/d5/6w9t_lzx3plbpmk6zpyz93dm0000gp/T//SD00791.000000"
      
      . mata for (j=1;j<=3;j++) {
      > j
      > }
        1
        2
        3
      
      .
      end of do-file

      Comment


      • #4
        Thank you Mike, thank you John for your answers.
        John, your workaround is something I didn't think of and I'll certainly keep it in mind.

        Let me add that I'm not surprised that what I want to achieve is not possible at the moment. It is my understanding that when you execute some lines of code from the do editor, these are saved in a temporary file and then executed via Stata's -do- command, which becomes illegal if Mata environment is already on (or better said: the -do- command is interpreted by Mata as a part of "do ... while" instructions).

        So, I guess this is actually more of a kind request to Stata Corp. to add a way of executing Mata code from the do-file editor that would work in this scenario. I know it's subjective, but I find extremely useful in many circumstances (eg: teaching, debugging) to be able to run R code line-by-line directly from an R script.
        Last edited by Andrea Discacciati; 08 Oct 2018, 04:55.

        Comment


        • #5
          Yes! One of my frustrations when working with Mata is that it doesn't work well with the dofile editor at all. Maybe Stata could introduce Ctrl+E for "execute mata code"?

          Comment

          Working...
          X