How to determine application exe name at run time?

11 views (last 30 days)
I want to determine the executable file name and path for a Matlab Compiler produced standalone application at run time. The desired result would be similar to mfilename('fullpath') but return the location of the exe file instead.

Accepted Answer

Guillaume
Guillaume on 10 Mar 2015
On Windows, on a computer with .Net, this may work:
args = System.Environment.GetCommandLineArgs;
fullpath = args(1);
I don't have the matlab compiler, so can't test.
  2 Comments
Chris Nolen
Chris Nolen on 10 Mar 2015
Thanks to both Guillaume and Image Analyst for your answers. Really, one of the answers supplied the exe name and the other gave its path. The following code is my pieced-together implementation. It appears to be working with R2014b on Windows.
function filename = exefilename
% Determine full path to calling file
% In a deployed program, this will point to the application exe.
% In regular Matlab, it will be the calling m-file.
if isdeployed % Executable in stand-alone mode.
[~, result] = system('path');
exedir = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
args = System.Environment.GetCommandLineArgs;
relpath = char(args(1));
[~,exename,ext] = fileparts(relpath);
filename = fullfile(exedir,[exename,ext]);
else % MATLAB mode.
[st,ind] = dbstack('-completenames');
filename = st(ind+1).file;
end
end

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 10 Mar 2015
See this function. Call it in your code and see what it returns.
% Returns the folder where the compiled executable actually resides.
function [executableFolder] = GetExecutableFolder()
try
if isdeployed
% User is running an executable in standalone mode.
[status, result] = system('set PATH');
executableFolder = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
% fprintf(1, '\nIn function GetExecutableFolder(), currentWorkingDirectory = %s\n', executableFolder);
else
% User is running an m-file from the MATLAB integrated development environment (regular MATLAB).
executableFolder = pwd;
end
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
uiwait(warndlg(errorMessage));
end
return;

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!