Announcement

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

  • Run Matlab from Stata

    Hi,

    I'm implementing a big data task. For now, I'm using both Stata and Matlab: I run a Stata do file first, and at some intermediate step I run a matlab script to get necessary data, and return to Stata to continue the task. (So basically I have 3 pieces of code scripts now.)

    Can I unify the scripts in one big Stata do-file of the form:

    First batch of Stata commands
    Run Matlab script, save the data
    Second batch of Stata commands

    and run that big do-file without interruption with just one click.

    Thank you.

    Green

  • #2
    Mr./Mrs./Miss Peas, If Matlab can be called from a shell (which I believe it can) then you should probably have no problem calling it from within Stata. Run -help shell- for the details. However healthy, please consider the inedible variety as a user ID. Real full names are strongly preferred. See the FAQ. You can contact the site administrators to request a change.
    You should:

    1. Read the FAQ carefully.

    2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

    3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

    4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

    Comment


    • #3
      Sorry for the name. I was just aware of that real name recommendation after creating the account. I'm contacting to change the username.

      Thank you for your answer, Roberto. I'm wondering if Stata should hold to wait the matlab run to finish, before the second part of Stata script continue?



      Comment


      • #4
        You say that you need to have the MATLAB script run before the second Stata batch, and so the answer is, "Yes, Stata should hold to wait." The command's help file (to which Roberto already referred you), says that shell will wait.

        Comment


        • #5
          Thanks for the ID-change.

          To wait, or not to wait, that is the question -

          Whether Stata should wait or not for the external program to finish, depends on the nature of your complete run. If the second batch of Stata commands takes the saved data created by Matlab as an input, then you need Stata to hold. Otherwise, it may not be necessary.

          There are three possibilities (explained in help shell):

          1. shell
          2. winexec
          3. xshell

          Use the one that fits your problem.

          (By the way, Stata has its own matrix programming language: Mata. Run help mata for details.)
          You should:

          1. Read the FAQ carefully.

          2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

          3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

          4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

          Comment


          • #6
            Many thanks Joseph and Roberto. I pretty much know to how go about now with your answers.

            Comment


            • #7
              If you use Windows, keep in mind that - at least to the best of my understanding, and I sincerely hope I am wrong - if you are running your do-files in batch mode, you cannot use shell commands within those do-files.

              See
              http://www.stata.com/statalist/archi.../msg00537.html
              and
              http://www.stata.com/statalist/archi.../msg01232.html

              Comment

              Working...
              X