Matlab and Stata in a Shell file

I wrote my first runall.sh file and it took me longer than it should have to make it work. I am therefore writing it down for future reference and maybe it also helps some other people. The task was to have a shell file that I could run from Atom on my Mac and it should execute some Matlab and Stata code. Here is what I had to do.

1) Making sure Matlab and Stata are both added to the path

On a Mac that means adding the following two lines in the .profile file which is located at /Users/yourusername/. You might have to adjust the code to fit the location and version of your installed software.

export PATH=$PATH:/Applications/Stata/StataMP.app/Contents/MacOS/
export PATH=$PATH:/Applications/MATLAB_R2017a.app/bin

2) Install script package in Atom

To execute the shell file in Atom, I installed the script package. It’s default keybinding was conflicting with another package that I had installed, so I had to change it by adding the following two lines to my keymap file.

'atom-text-editor':
'alt-cmd-enter': 'script:run'

3) The shell file

This is my final shell file saved as runall.sh in my project folder.

cd "/Users/path_to_/matlab_and/stata_files/"

matlab -nosplash -nodisplay -nodesktop -r "run Step1.m; quit" > matoutfile

StataMP -b do Step2.do

The crucial part was to add the ; quit. Without it, the switching from Matlab to Stata wouldn’t work. I chose to set the working directory in the beginning. Otherwise I would have to make sure to start Atom from the command line to make sure the script package starts the code from the right directory.

2 thoughts on “Matlab and Stata in a Shell file

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s