×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

how to know the current m-file directory in the mfile

how to know the current m-file directory in the mfile

how to know the current m-file directory in the mfile

(OP)
I want to open a file with the path like this [current m-file dir]\file\filename.txt.  I found i cannot do like just textread('file\filename.txt'..) in the m-file. so i think i should know the m-file directory itselt first, then do the job. do anyone know how to do this? thank you!

RE: how to know the current m-file directory in the mfile

pwd, I think

Cheers

Greg Locock

Please see FAQ731-376 for tips on how to make the best use of Eng-Tips.

RE: how to know the current m-file directory in the mfile

(OP)
no, when pwd used in m-file, it still returns the current work directory of matlab, not the m-file directory itself.

RE: how to know the current m-file directory in the mfile

(OP)
i got an ugly way,

s = mfilename;
s = which(s);
then delete the mfile name in the string s.....

RE: how to know the current m-file directory in the mfile

o.O

Use / instead of \

try something like

cd('c:/path')
textread('filename')

This what you needed?


BsK

RE: how to know the current m-file directory in the mfile

(OP)
no. I need to know the current running m-file path dynamically to keep the m-file function has better adaptability when moving the m-file.

RE: how to know the current m-file directory in the mfile

Another approach, which appears to be more gruesome, is to use depdir on the current file.  Unfortunately, it takes a while, since it crunches through the entire path and the last entry is the current directory of the m-file.

TTFN



Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources