Announcement

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

  • How to execute a Matlab script from Stata

    Hi all,

    I'd like to run a Matlab script from within my Stata Do-File. Haven't done this before, but shell appears to be the right Stata command for my needs (the Do-File should wait until the Matlab script is finished). However, using shell my Do-File only opens the Matlab script and, unfortunately, doesn't actually execute it without me pressing "Run" in Matlab.

    At the moment I'm stuck with:
    Code:
    di "Do-File begins now"
    shell C:\Users\...\MyMatlabScript.m
    di "Do-File complete!"
    I assume I'm missing out on some command following shell... Probably one that executes the Matlab script and quits Matlab in the following. Has anyone done this before and could point me to the right direction?

    Best
    Christian

  • #2
    If you double click the .m file outside of Stata, does it run? I wonder if you need something like

    shell matlab myfile.m
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    StataNow Version: 19.5 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

    Comment


    • #3
      Yes, the .m file itself works from within Matlab. Edit: Double-clicking the .m just opens the code without running it (as with Stata Do-Files).

      I've found a solution in the meantime that works for me:
      Code:
      shell matlab -noFigureWindows -r "try; run('C:\Users\...\MyMatlabScript.m'); catch; end; quit"
      Not sure what the additional options do, so if someone stumbles upon this thread and shares the insights I'd be fully enlightened. ;-)
      Last edited by Christian Agethen; 29 Jul 2019, 08:21.

      Comment


      • #4
        The documentation of Matlab command-line options is here: https://www.mathworks.com/help/matla...abwindows.html

        With Stata 16 and Python integration, you may also use the subprocess module to do the call, possibly through a pipe if Matlab supports this.
        Another possibility is to use the Pywin32 package to control Matlab through COM automation (the same kind of automation that is provided by Excel or Stata).
        Or you may use Matlab's COM client to control Stata from a Matlab script, or control both Matlab and Stata from a Python script...

        Comment

        Working...
        X