Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the location of a deployed Matlab application at runtime for Mac and Linux

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 Linux
  • matlabroot: location of the MCR
  • pwd: user's current folder, which may differ from the executable's location when it's run with a full path
  • dbstack: location of unpackaged .m file
  • which: location of unpackaged .m file
  • fileattrib: location of unpackaged .m file
like image 930
ackrause Avatar asked Jul 25 '13 18:07

ackrause


People also ask

How install Matlab Runtime in Linux?

Unzip the MATLAB Runtime installer at the terminal using the unzip command. Start the MATLAB Runtime installer. Double-click the file setup.exe from the extracted files to start the installer. sudo is only required if you install to a directory that you do not have write access to.

What is Matlab runtime compiler?

The MATLAB Compiler Runtime (MCR) enables you to run applications compiled within MATLAB using MATLAB Compiler. MCR does not require a MATLAB license and can be used to run the MATLAB compiled program on computers which do not have MATLAB installed.

What is Matlab run time?

MATLAB Runtime (MCR) is a collection of shared libraries, MATLAB code, and other files that enables the execution of compiled and packaged MATLAB applications on systems without an installed version of MATLAB.


2 Answers

Mac: To get the location of an installed executable MyDeployedApplication.app on Mac from within the deployed app itself, try the following:

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

This solution uses the 'ps', 'grep', and 'top' terminal commands, assumes the user has a single instance of MyDeployedApplication.app currently running, and has been tested on MAC OS Yosemite 10.10.5 with MATLAB Compiler 2015a only.

Note: While pgrep worked to return the PID of the deployed, currently running application from outside of the application (directly in Terminal or in an open MATLAB session), it did not return anything from within the application. Hence the use of top and grep.

Linux: To get the path of the installed executable on Linux, change the syntax of Sam's answer to Linux style:

[status, result] = system('echo $PATH');
realpwd = char(regexpi(result, '(.*?):', 'tokens', 'once'));        
like image 182
Marianne Avatar answered Sep 28 '22 09:09

Marianne


Does the function ctfroot do what you need?

ctfroot is a command from MATLAB Compiler. From the documentation:

root = ctfroot returns a string that is the name of the folder where the deployable archive for the deployed application is expanded.

You probably want to use the command ctfroot only within an if isdeployed block.

Edit

If you need the location of the executable, rather than the location to which it is expanded, you can use the following function:

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

This works as the path to the executable is added to the PATH variable as the first entry by the executable at runtime.

Alternatively, you can create a MEX file that will perform a similar job. See this MathWorks support answer for more details, and for an example MEX file.

like image 36
Sam Roberts Avatar answered Sep 28 '22 07:09

Sam Roberts