fopen a file from another directory location

조회 수: 49 (최근 30일)
Yohann Cornilliere
Yohann Cornilliere 2018년 5월 23일
편집: Stephen23 2021년 7월 5일
Hi,
I am trying to read a .txt file in another directory than my matlab workspace by using fopen. I found a lot of thing on mathworks but nothing work. Someone can help me? It mustn't be that difficult I suppose...
  댓글 수: 4
Ameer Hamza
Ameer Hamza 2018년 5월 23일
@Yohann, fopen does not load the content of the file. To read the content use a function like textscan(), fscanf() etc
Stephen23
Stephen23 2018년 5월 23일
"Yes I tried this but the output variable of the fopen is 3 (I don't know why)"
Because 3 is the file identifier for the open file, exactly as the fopen documentation describes: "opens the file...and returns an integer file identifier equal to or greater than 3."
To know what functions and operators do is why you should read the documentation.

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

답변 (2개)

Ameer Hamza
Ameer Hamza 2018년 5월 23일
You can use a relative path as follow
f = fopen('../../filename');
symbol .. means one folder up. For example, if you have a structure like this
folder1
file1.txt
folder2
file2.txt
folder3
current directory <---- you are here
To access the file1 use
f = fopen('../../file1.txt')
and to open file2.txt use
f = fopen('../file2.txt')
this is especially useful if you want to move your project folder as this will not break the paths on any other system.
The second option is to use absolute paths as given by @Paolo in the comments.
  댓글 수: 3
Ameer Hamza
Ameer Hamza 2018년 5월 23일
That is mostly dependent on the preference of the user. In absolute path case, it will be necessary to change rootdir whenever moving the project. Whereas in relative case, it is always easy to construct a full path by using pwd without asking the user to change anything.
Stephen23
Stephen23 2021년 7월 4일
편집: Stephen23 2021년 7월 5일
@Ameer Hamza: pwd is not required, simply specify the current directory using '.'.

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


Guillaume
Guillaume 2018년 5월 23일
the output variable of the fopen is 3
That is expected and means that fopen is succesful.
if I open the same file in the matlab workspace that variable is full of informations
Then you're not using fopen. fopen, as documented opens a file for reading (or writing) and returns a file identifier. It never returns the content of the file. You then have to read the content with some other functions such as textscan or fread.
If you don't want the hassle, there are plenty of other ways to import a file in one go.
Regardless of the function you use, all of them support using absolute or relative paths as explained by others.
  댓글 수: 2
priyanshu ashiya
priyanshu ashiya 2021년 7월 4일
편집: priyanshu ashiya 2021년 7월 4일
im facing the same problem
i m running gazealyse matlab toolbox.
error i get is
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
tried to run matlab as admin didn't work.
any insight is helpful.
Image Analyst
Image Analyst 2021년 7월 4일
@priyanshu ashiya What is the value of fid? If it's -1, the file was not able to be opened for some reason.

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

카테고리

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

제품


릴리스

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by