pwd doesn't give actual location of the file that called it

조회 수: 21 (최근 30일)
Dylan
Dylan 2021년 10월 26일
댓글: Dylan 2021년 10월 27일
I have a file 'file.m' in the folder '/Users/userA/Documents/MATLAB/a/b/c/'. When I call pwd within the file (there are no other lines of code) it returns '/Users/userA/Documents/MATLAB'. How do I get the actual file location? I am on MacOS.

답변 (2개)

chris crowley
chris crowley 2021년 10월 26일
How are you running this code?
pwd displays the current working directory. If you click the play button on this code and it is not located in whatever folder you currently have MATLAB navigated to, it will ask you if you want to add it to the path or change current working folder. If you add to path, the code can run WITHOUT changing your current working folder. If this prompt did not come up it suggests that you already have /Users/userA/Documents/MATLAB/a/b/c/ added to your path.
I think you want to replace pwd with
p = mfilename('fullpath')
disp(p)
I suspect this is what is going on, but would need more info about specifically how you are running your code to really get to the bottom of this.
  댓글 수: 2
Dylan
Dylan 2021년 10월 26일
mfilename('fullpath') gives me something bizarre that I didn't create.
/private/var/folders/j1/q3gjch0d73n5p0v58fjx7zt40000gn/T/Editor_odzei/LiveEditorEvaluationHelperE661157072
I see, so pwd give the location in the file directory on the left hand side of the matlab GUI, and doesnt have to do with the file it's being run from. Interesting choice by the devs I have to say.
chris crowley
chris crowley 2021년 10월 26일
yeah, that is my understanding.

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


Steven Lord
Steven Lord 2021년 10월 26일
Giving the location of the currently running file is not the purpose of the pwd function. Its purpose is to give the location of the current working directory, which is independent of what file is running (or even if a file is running.)
If you're evaluating a section of a Live Script, mfilename will return the name of a temporary file used to evaluate that section of the Live Script not the name of the Live Script itself.
How are you planning to use the information about where the file being executed is located? Are you trying to use it to determine where to create a data file (with the save function)? Are you trying to use it so you can read in the program file itself (which may pose some challenges, especially for Live Scripts or if you hope to do this in a standalone application created by MATLAB Compiler)? Or do you have some other purpose in mind (and if so, what)?
  댓글 수: 2
Dylan
Dylan 2021년 10월 26일
I would like to save and read documents within the same folder as the code and in subfolders.
Dylan
Dylan 2021년 10월 27일
@Steven Lord Do you know if this is possible?

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

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by