how can I figure out the path of the compiled exe.
The command "pwd" gives me the workdirectory. But the workdirectory is not always the same like the application path.
In my case I register at windows a certain extension to open theses files with my compiled program. Opening the the program like this, the workdirectory is the directory of the file with the extension, that was called and not the directory of the exe.
At the moment I pass to the exe 2 args:
1. The name of the file, that should be processed by the exe
2. The name of path of the exe.
It bothers a little bit, that I have to give the 2nd parameter, because the exe should know, where it is.
Andreas
it is not quite clear what you need...
however, did you look at these to set the path to your exe
help addpath; % ML environment
help setenv; % OS environment -> add to existing path
help getenv; % OS environgment -> get existing path
us
function pname=AppRootPath(varargin)
% pname = AppRootPath()
% return the Root path of an application
%
% pname = AppRootPath(subfolder) pad the subfolder to the result
thisfile=mfilename('fullpath');
[pname] = fileparts(thisfile);
if isdeployed
for upstep=1:2
pos=find(pname==filesep, 1, 'last');
pname=pname(1:pos-1);
end
end
[pname] = strcat([pname filesep],varargin{:});
% Bruno
I tried your function.
The extracted path is the cache directory, to which Matlab extracts its files, located in
C:\Documents and Settings\username\Local Settings\Temp\username\mcrCache7.10\progname\
But it is not the path of the exe, that is located in
C:\Program Files\ProgramFolder
Andreas
Ah too bad! It looks like the CTF files are extracted at different location from 2008A (I still use 2006B). This breaks the logic of my function. I don't know how to get the path in 2008A. Sorry.
Bruno
function testPath()
% mcc -mv testPath
disp(pwd);
disp(AppRootPath())
input('Press return');
end
After compiling I put the application to
D:\Projects\testPath\testPath.exe
If I run it, I get the following output:
D:\Projects\testPath
C:\DOCUME~1\username\LOCALS~1\Temp\username\mcrCache7.10\
Press return
Now I create a shortcut on the desctop. After creating the shortcut, I open the property menu and write
in the "Target": D:\Projects\testPath\testPath.exe
in the "Start in": C:\WINDOWS
Now the work directory is different to the application directory.
The output of the program is:
C:\WINDOWS
C:\DOCUME~1\agreuer\LOCALS~1\Temp\agreuer\mcrCache7.10\
Press return
But I would expect:
C:\WINDOWS
D:\Projects\testPath
Press return
This is system dependent and if you can't do it using Matlab commands (which
seems you tried and could not), then may be this page below will help as it
tells how to do it from C or by having access to argv[0] (you might have to
write some C function and mex it)
See this:
http://stackoverflow.com/questions/143174/c-c-how-to-obtain-the-full-path-of-current-directory
--Nasser
Like us says, another more flexible option might be to have your
installer program (I use Wise) make an environment variable that is
the folder where you user installed to (which could be anything and
not the assumed one like I did). Then your folder could query this
environment variable and base all your files and subfolders off that.
I agree with you that it seems like the executable should know where
it lives and we shouldn't need to do these weird workarounds that
involve installers setting up things in a special way, but if there is
a better way, I'm still looking for it. Maybe there's some java trick
that you can call in your startup code - I don't know.
You might be able to set some properties of the icon and that might
work as long as they start your program by clicking the icon. If they
click the exe directly from Windows Explorer, then those icon
properties won't apply and you'll be out of luck.
This is a good question, that for me at least, still doesn't have a
perfect answer. I had called MATLAB tech support about this about a
year ago and they didn't really have an answer.
Regards,
ImageAnalyst
>
> This is system dependent and if you can't do it using Matlab commands (which
> seems you tried and could not), then may be this page below will help as it
> tells how to do it from C or by having access to argv[0] (you might have to
> write some C function and mex it)
>
MEX won't work because it will packed in the CTF extraction cache folder.
Bruno
Maybe this solution can help:
How can I find the directory containing my compiled application?
http://www.mathworks.com/support/solutions/en/data/1-6OFV37/
Aur?lien
thanks, this seems to be the solution.
function [thePath]=showpath()
% Show EXE path:
if isdeployed % Stand-alone mode.
[status, result] = system('set PATH');
thePath = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
else % Running from MATLAB.
thePath=pwd;
end
return
My test program gives the following output:
pwd: C:\WINDOWS
AppRootPath: C:\DOCUME~1\username\LOCALS~1\Temp\username\mcrCache7.10\
userpath: C:\Documents and Settings\username\My Documents\MATLAB;
showpath: D:\Projects\testPath
The test program was
function testPath()
% mcc -mv testPath
disp(['pwd: ' pwd]);
disp(['AppRootPath: ' AppRootPath()])
disp(['userpath: ' userpath])
disp(['showpath: ' showpath()]);
input('Press return');
end
Andreas
----------------------------------------------------------------------------------------------------------------
Where does AppRootPath come from? It doesn't seem to be a built-in
MATLAB function.
Thanks,
ImageAnalyst
> ----------------------------------------------------------------------------------------------------------------
> Where does AppRootPath come from? It doesn't seem to be a built-in
> MATLAB function.
ImageAnalyst, it's mine (post #3). It works for compiler with CTF unzipped at the same folder as the EXE, e.g., Matlab 2006A/2006B. More recent MATLAB compilers unzip CTF somewhere else. I do not have compiler license for recent Matlab.
Bruno
Thanks Bruno - I see it now. I'll look into if it's still applicable,
or what it does, later. I do know that now with >=R2008b you no
longer need to distribute the CTF file to the target computer for the
executable to run.