title: Get the location of a deployed Matlab application at runtime for Mac and Linux
body: <p>I have some standalone Matlab programs that for different reasons need to access files in the directory they're located (either to launch another program or to read some XML files there). I have the following function that works for Windows:</p>
  <pre><code>function execDir = get_deployed_exec_dir()
  % Returns the directory of the currently running executable, if deployed,
  % an empty string if not deployed (or if unable to determine the directory)
  execDir = '';
  if isdeployed
      [status, execDir] = system('path');
      if status == 0
          execDir = char(regexpi(execDir, 'Path=(.*?);', 'tokens', 'once'));
      end
  end
  </code></pre>
  <p>To get it to work for Linux and Mac I figured I could replace <code>system('path')</code> with <code>system('echo $PATH')</code> and alter the regular expression to fit Unix syntax, but unlike with Windows the directory of the currently running executable doesn't seem to be automatically added to the front of the path variable. Is there a way within Matlab to get the directory of the currently running executable (I know there is for the script, but that doesn't seem to work properly when deployed), or should I edit the script that sets up the MCR before running the application to set a variable that my code can read with the <code>system</code> command?</p>
  <p>For concreteness, somewhere on the user's computer is the folder <code>EXECFOLDER</code>, with the structure:</p>
  <pre><code>EXECFOLDER
  | exec1
  | exec2
  | run_exec1.sh
  | run_exec2.sh
  | data.xml
  </code></pre>
  <p>I want to figure out the path to <code>EXECFOLDER</code> regardless of where the user is running <code>run_exec1.sh</code> (script that sets up the MCR and calls <code>exec1</code>), so that <code>exec1</code> can read from <code>data.xml</code> and execute <code>exec2</code>.</p>
  <h3>Summary Of Attempts:</h3>
  <ul>
  <li><code>system('echo $PATH')</code>: executable directory is not on the path in Mac and Linux</li>
  <li><code>matlabroot</code>: location of the MCR</li>
  <li><code>pwd</code>: user's current folder, which may differ from the executable's location when it's run with a full path</li>
  <li><code>dbstack</code>: location of unpackaged .m file</li>
  <li><code>which</code>: location of unpackaged .m file</li>
  <li><code>fileattrib</code>: location of unpackaged .m file</li>
  </ul>