MATLAB: How to find the directory containing the compiled application

compilerctfrootdirectoryexecutablefolderMATLAB Compilermatlabrootpwdruntimestandalone

How can I find the directory containing my compiled application? The CTF data has been extracted into my user Application Data directory, but neither "ctfroot", "matlabroot" nor "pwd" show the actual location of the compiled executable.

Best Answer

On Windows there are two possible ways to achieve this:
Option 1:
Extract the path from the environment variable PATH, this can be achieved as in the following example:
function currentDir = getcurrentdir
if isdeployed % Stand-alone mode.
[status, result] = system('path');
currentDir = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
else % MATLAB mode.
currentDir = pwd;
end
Please note that using the function "getenv" in the above example will not work. You will have to use the "system" command to get the PATH variable that contains the path to the executable as the first entry. This entry gets added automatically at runtime by the stand-alone.
Option 2:
The alternative, on Windows as well as all other supported platforms, would be to use a MEX-file. Attached is an example that illustrates how to locate the compiled executable. To compile the MEX file, please follow the steps detailed on this page of the documentation:
On Mac machines you can use the following workaround to get the path to the directory where your application sits:
if isdeployed && ismac
NameOfDeployedApp = 'MyDeployedApplication'; % do not include the '.app' extension
[~, result] = system(['top -n100 -l1 | grep ' NameOfDeployedApp ' | awk ''{print $1}''']);
result=strtrim(result);
[status, result] = system(['ps xuwww -p ' result ' | tail -n1 | awk ''{print $NF}''']);
if status==0
diridx=strfind(result,[NameOfDeployedApp '.app']);
realpwd=result(1:diridx-2);
else
msgbox({'realpwd not set:',result})
end
else
realpwd = pwd;
end