Switching work-dir to location of *.m-file

Hi there, I am fairly new to matlab, so excuse me if my question is trivial....
I have the following issue. I want to use a skript i use for several projects and which should used by several collegues via Subversion.
The script (Let call it Script A) is supposed to compile a few files and (If they are more recent) copy them in a sub-folder folder.
My problem is the following. The folder might be in different places in my collegues PCs, therefore i have to use relative paths to the compiler and the files. If i run the script in different places this works just fine. But if i want to get the newest version for my current project another script (Script B) is calling script A to check whether there is a new version, and ,if so, copy the compiled file to the project . As script B is running in the work-dir of the current project rather than in the dir of script A the relative paths don't fit anymore. The folder of Script A is in the search path of matlab and therefore can be called from the project. My first Idea is to switch to the dir. of script A execute it and then switch back to the work-dir of the project. The Question: Is there a command to change the current work-dir to the one of script A? Basically a command to get the actual folder of the *.m-file?
Thanks for all tips.

 채택된 답변

Fabian
Fabian 2012년 6월 12일

0 개 추천

Thanks for the hint, pinted me in the right direction.. The solution is: "fileparts(which('func'))" gives you the path of func. "fileparts(which(mfilename))" gives you the current path of an m-file.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2012년 6월 11일

0 개 추천

If it is a script and not a function, then you can use run() with a path; it changes directory in order to execute the script.
You can use which() to find out the path to a script or function.

댓글 수: 2

Avishek Dutta
Avishek Dutta 2012년 6월 12일
Hi all,
In this context I would like to ask if there is way to provide relative file names like "../test/data.txt" in dlmread.
Thanks
Walter Roberson
Walter Roberson 2012년 6월 12일
I believe so. Easy to test.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Adding custom doc에 대해 자세히 알아보기

질문:

2012년 6월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by