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
matlabroot
? And what is the state ofpwd
when the program launches? There are of course the *NIX commandswhich
andwhereis
, but these seem heavy-handed. – Bonannomatlabroot
provides you with the location of the MCR being used.pwd
gives you the location the user is in in the terminal (where they called the application), but for our program people tend to navigate to where their data is and call the program using it's full path, so where the executables actually are is different from the user's current directory. I was wondering if there's a way in Matlab to get that information without having to set up an environment variable in the Bash script you use to start up the program. – Dominicadominicalstack = dbstack('-completenames')
fullpath = stack.Name
to get the full path of the currently running program? Other things to try might be Matlab'swhich
(with'-all'
flag maybe) or maybefileattrib
. – Bonannodbstack
,which
, andfileattrib
all seem to give me the location where the.m
files are unpackaged for the MCR to run, rather than their original location in the executable. I'm beginning to think that that might be as good as you can do within Matlab code itself due to the way it's run. – Dominicadominicalsystem
andunix
function, so you may need to find a solution there (e.g.,which
,whereis
, and maybe even some form ofps
). Have you asked this question over at MatlabCentral? It seems like something that one should be able to obtain. – Bonannolsof
on OS X and and some other forms of UNIX and/proc/<PID>/
on Linux that might be helpful. – Bonanno