Thread Subject: Get current directory from EXE

Subject: Get current directory from EXE

From: Erik

Date: 4 Jul, 2008 20:35:02

Message: 1 of 5

Hello,

I am building a GUI in MATLAB that involves reading in
textfiles for setting values in the GUI. Since the textfiles
are in the same directory as the GUI, I just use 'pwd' in
MATLAB to get the directory to load the files.

The problem is, once I compile this GUI as an EXE (using the
MCR) the 'pwd' command returns C:\My Documents\MATLAB,
instead of P:\DRAG_GUI (which it should be), causing my
program to crash. I have tried to use GetCurrentDirectory()
in C then pass it to MATLAB, but it also returns C:\My
Documents\MATLAB, which may I add, does not even exist. How
do I get the actual current directory??

Thanks,
Erik

Subject: Get current directory from EXE

From: Aleks

Date: 4 Jul, 2008 23:27:01

Message: 2 of 5

"Erik " <emiehling@gmail.com> wrote in message
<g4m1hm$n1l$1@fred.mathworks.com>...
> Hello,
>
> I am building a GUI in MATLAB that involves reading in
> textfiles for setting values in the GUI. Since the textfiles
> are in the same directory as the GUI, I just use 'pwd' in
> MATLAB to get the directory to load the files.
>
> The problem is, once I compile this GUI as an EXE (using the
> MCR) the 'pwd' command returns C:\My Documents\MATLAB,
> instead of P:\DRAG_GUI (which it should be), causing my
> program to crash. I have tried to use GetCurrentDirectory()
> in C then pass it to MATLAB, but it also returns C:\My
> Documents\MATLAB, which may I add, does not even exist. How
> do I get the actual current directory??
>
> Thanks,
> Erik

If you know where files are can you do something like
cd('P:\drag_gui')
or have user enter the directory (you can even make a browse
button and all) where files reside.

Aleks

Subject: Get current directory from EXE

From: Titus

Date: 7 Jul, 2008 10:08:42

Message: 3 of 5


"Erik " <emiehling@gmail.com> schrieb im Newsbeitrag
news:g4m1hm$n1l$1@fred.mathworks.com...
> Hello,
>
> I am building a GUI in MATLAB that involves reading in
> textfiles for setting values in the GUI. Since the textfiles
> are in the same directory as the GUI, I just use 'pwd' in
> MATLAB to get the directory to load the files.
>
> The problem is, once I compile this GUI as an EXE (using the
> MCR) the 'pwd' command returns C:\My Documents\MATLAB,
> instead of P:\DRAG_GUI (which it should be), causing my
> program to crash. I have tried to use GetCurrentDirectory()
> in C then pass it to MATLAB, but it also returns C:\My
> Documents\MATLAB, which may I add, does not even exist. How
> do I get the actual current directory??
>
> Thanks,
> Erik

Hi Erik,

the current directory is the one where you find a freshly started MATLAB,
usual the work directory or home directory. But the function ctfroot could
help you, moving one directory higher is what you are looking for (therefore
the fileparts): put this at the beginning of your mfile:

if isdeployed
  directory_where_exe_and_ctf = fileparts(ctfroot);
  cd (directory_where_exe_and_ctf)
end

Titus

Subject: Get current directory from EXE

From: varun dj

Date: 27 Oct, 2009 04:10:23

Message: 4 of 5

Hi
I tried to use getcwd in S-Function but its not working...
What should be done in S-Function to know its current working directory...

Subject: Get current directory from EXE

From: Jan Simon

Date: 27 Oct, 2009 09:06:03

Message: 5 of 5

Dear varun dj!

> I tried to use getcwd in S-Function but its not working...
> What should be done in S-Function to know its current working directory...

GETCWD does reply the "current working directory". But this is, as the name says, the directory, which you are currently working in, and *not* the parent-directory of the currently processed function!

I'm not using Simulink, so perhaps my idea does not work. But inside Matlab, the function WHICH can find a file by its name and searches also in the folder of the currently processed function. In addition FOPEN(FileName, 'r') searchs in the entire Matlab path also - even if the FileName includes a path! So you can omit the pathz and find the file!

By the way: FOPEN(FileName, 'r+') does not dig in all folders of the PATH, therefore it is much faster! (At least under Matlab 6.5, 7.7, Windows)

Finally I'd strongly recomment to split functions and data and store the data in a neutral folder, e.g. PREFDIR or TEMPDIR. Then your program does not depend on volatile folder names.

Kind regards, Jan

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
pwd Erik 4 Jul, 2008 16:35:05
getcurrentdirec... Erik 4 Jul, 2008 16:35:05
mcr Erik 4 Jul, 2008 16:35:05
rssFeed for this Thread

Contact us at files@mathworks.com