필터 지우기
필터 지우기

How can I open a file to be read, from a relative file location?

조회 수: 42 (최근 30일)
Connor Sherod
Connor Sherod 2018년 7월 10일
편집: Yash Trivedi 2018년 7월 10일
The function which reads from the text file is located at:
C:\Users\x\x\x\MatlabFiles\Connor_Matlab
and it needs to open:
%C:\Users\x\x\x\OUTPUT\Square\M1CR100S4\(AllFld_SVE).OALLFLD
Right now I've tried the relative path. I cannot write it as an absolute path, as it needs to be opened on multiple different computers.
fID = fopen('..\OUTPUT\Square\M1CR100S4\(AllFld_SVE).OALLFLD','r');

채택된 답변

Yash Trivedi
Yash Trivedi 2018년 7월 10일
Hi Connor,
According to the relative path in your fopen() call, you need to go one level higher up in the directory hierarchy.
fID = fopen('..\..\OUTPUT\Square\M1CR100S4\(AllFld_SVE).OALLFLD','r') should work.
A single .. will look for the 'OUTPUT' folder in the 'MatlabFiles' folder and not in the last 'x' folder.
  댓글 수: 2
Connor Sherod
Connor Sherod 2018년 7월 10일
Thank you!
So if I had the file located 4 folders back would I need to repeat that 4 times and then proceed from there? Essentially the '..\' acts as taking it back a single step?
Yash Trivedi
Yash Trivedi 2018년 7월 10일
편집: Yash Trivedi 2018년 7월 10일
Yes, that's right. The '..\' acts as taking it back a single step. You might want to read through this thread which suggests alternatives and potential pitfalls with relative paths, especially if you're going up four levels of hierarchy -

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by