Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: How can an m-file get its path?

Subject: How can an m-file get its path?

From: Dimitri Shvorob

Date: 8 Apr, 2008 14:47:04

Message: 1 of 7

..Put another way, can you write function 'where_am_i'
that, when run, would tell you in what directory it
resides? (No, 'cd' or 'pwd' won't work).

Thanks a lot!


Subject: How can an m-file get its path?

From: French Caro

Date: 8 Apr, 2008 14:55:05

Message: 2 of 7

Hi
If I had understood the question...
When you are in the function you can access to its full name
with mfilename so also to its directory :
p = mfilename('fullpath') %gives the fullname
fileDirectory = fileparts(p) %gives the directory of the
current running m-file

"Dimitri Shvorob" <not.dimitri.shvorob@gmail.com> wrote in
message <ftg0h8$gs6$1@fred.mathworks.com>...
> ..Put another way, can you write function 'where_am_i'
> that, when run, would tell you in what directory it
> resides? (No, 'cd' or 'pwd' won't work).
>
> Thanks a lot!
>
>

Subject: How can an m-file get its path?

From: Michael Katz

Date: 8 Apr, 2008 14:57:07

Message: 3 of 7

p = mfilename('fullpath') returns the full path and name of the M-file in
which the call occurs, not including the filename extension.

"Dimitri Shvorob" <not.dimitri.shvorob@gmail.com> wrote in message
news:ftg0h8$gs6$1@fred.mathworks.com...
> ..Put another way, can you write function 'where_am_i'
> that, when run, would tell you in what directory it
> resides? (No, 'cd' or 'pwd' won't work).
>
> Thanks a lot!
>
>


Subject: How can an m-file get its path?

From: Randy Poe

Date: 8 Apr, 2008 15:01:30

Message: 4 of 7

On Apr 8, 10:47 am, "Dimitri Shvorob" <not.dimitri.shvo...@gmail.com>
wrote:
> ..Put another way, can you write function 'where_am_i'
> that, when run, would tell you in what directory it
> resides? (No, 'cd' or 'pwd' won't work).
>
> Thanks a lot!

Perhaps something involving WHICH. That will work
if your function is on the path and it is the only function
(or the first in precedence) by that name on the path.

Ah! Found your answer while investigating the HELP
for another useful function, INPUTNAME.

The function you want is MFILENAME:

P = mfilename('fullpath');

                    - Randy

Subject: How can an m-file get its path?

From: Dimitri Shvorob

Date: 8 Apr, 2008 15:14:03

Message: 5 of 7

Problem solved, thanks to everyone who responded!

Subject: How can an m-file get its path?

From: Jos

Date: 8 Apr, 2008 21:29:15

Message: 6 of 7

"Dimitri Shvorob" <not.dimitri.shvorob@gmail.com> wrote in
message <ftg0h8$gs6$1@fred.mathworks.com>...
> ..Put another way, can you write function 'where_am_i'
> that, when run, would tell you in what directory it
> resides? (No, 'cd' or 'pwd' won't work).
>
> Thanks a lot!
>
>

Others have pointed you (rightfully!) to MFILENAME. I just
wanted to suggest that DBSTACK may also be useful,
especially in subfunctions.

Jos

Subject: How can an m-file get its path?

From: Jos

Date: 8 Apr, 2008 21:29:15

Message: 7 of 7

"Dimitri Shvorob" <not.dimitri.shvorob@gmail.com> wrote in
message <ftg0h8$gs6$1@fred.mathworks.com>...
> ..Put another way, can you write function 'where_am_i'
> that, when run, would tell you in what directory it
> resides? (No, 'cd' or 'pwd' won't work).
>
> Thanks a lot!
>
>

Others have pointed you (rightfully!) to MFILENAME. I just
wanted to suggest that DBSTACK may also be useful,
especially in subfunctions.

Jos

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
path directory Dimitri Shvorob 8 Apr, 2008 10:50:05
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics