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:
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
To get it to work for Linux and Mac I figured I could replace system('path')
with system('echo $PATH')
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 system
command?
For concreteness, somewhere on the user's computer is the folder EXECFOLDER
, with the structure:
EXECFOLDER
| exec1
| exec2
| run_exec1.sh
| run_exec2.sh
| data.xml
I want to figure out the path to EXECFOLDER
regardless of where the user is running run_exec1.sh
(script that sets up the MCR and calls exec1
), so that exec1
can read from data.xml
and execute exec2
.
Summary Of Attempts:
system('echo $PATH')
: executable directory is not on the path in Mac and Linuxmatlabroot
: location of the MCRpwd
: user's current folder, which may differ from the executable's location when it's run with a full pathdbstack
: location of unpackaged .m filewhich
: location of unpackaged .m filefileattrib
: location of unpackaged .m file