필터 지우기
필터 지우기

Running script from command line changes working directory to script's directory

조회 수: 23 (최근 30일)
Renat
Renat 2018년 4월 30일
댓글: Fangjun Jiang 2018년 4월 30일
Dear all,
I have a problem keeping the working directory when running a script from command line. I have a script saved in my Matlab script directory, and I call it with
matlab -nodisplay -nodesktop -r "run('/path/to/Matlab/scripts/folder/the_script.m')"
The script is supposed to work on some files in the current directory, but when I call it from within the directory, it cannot find the files because Matlab's working directory changes to where the script is. How can avoid that without changing directory back to my working directory implicitly in the script?
  댓글 수: 2
Renat
Renat 2018년 4월 30일
편집: Renat 2018년 4월 30일
Looks like using the full path cause Matlab to change working directory. Removing it fixed the issue. Case closed!
Fangjun Jiang
Fangjun Jiang 2018년 4월 30일
The behavior you observed is caused by run(). You can see "help run" to find the description.
-r option means to execute a statement, a function or a script. You could use below, without full folder specification if it is in the MATLAB path.
matlab -nodisplay -nodesktop -r the_script

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

답변 (1개)

Fangjun Jiang
Fangjun Jiang 2018년 4월 30일
In this case, I would suggest adding your MATLAB script directory into MATLAB path.
  댓글 수: 1
Renat
Renat 2018년 4월 30일
It is in the path - that scripts folder is exactly for that.
And it just occurred to me that maybe I shouldnt write the whole path there. And indeed - I just tried it without the path and Matlab doesnt change the working directory when invoking the script.

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

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by