when I run an m-file, Is there a way to "change current folder" to the folder that this m-file is placed, by means of some commands at the beginng of the file ?

조회 수: 8 (최근 30일)
I have a m-file, for example mytest.m and at the beginning of it , I would like to place a command (or commands), in order to "change current folder" into the folder that mytest.m is placed. This change of the folder has to be done, provided that we don't know the folder of mytest.m.
Is it feasible ?
Thanks in advance
  댓글 수: 1
Stephen23
Stephen23 2018년 9월 17일
"Is it feasible ?"
Feasible... yes.
Good idea... unlikely.
Changing directories is slow, and makes debugging much harder.
All MATLAB functions that read/write files accept absolute/relative filenames (which are much more efficient than changing directories), so why not just do that?

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

채택된 답변

Walter Roberson
Walter Roberson 2018년 9월 17일
mfileinfo = mfilename('fullpath');
mfiledir = fileparts(mfileinfo);
olddir = pwd;
onCleanup(@() cd(olddir));
cd(mfiledir);
The above will also cd back to the previous directory when the function exits.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by