fopen a file from another directory location

48 visualizzazioni (ultimi 30 giorni)
Yohann Cornilliere
Yohann Cornilliere il 23 Mag 2018
Modificato: Stephen23 il 5 Lug 2021
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 Commenti
Ameer Hamza
Ameer Hamza il 23 Mag 2018
@Yohann, fopen does not load the content of the file. To read the content use a function like textscan(), fscanf() etc
Stephen23
Stephen23 il 23 Mag 2018
"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.

Accedi per commentare.

Risposte (2)

Ameer Hamza
Ameer Hamza il 23 Mag 2018
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 Commenti
Ameer Hamza
Ameer Hamza il 23 Mag 2018
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 il 4 Lug 2021
Modificato: Stephen23 il 5 Lug 2021
@Ameer Hamza: pwd is not required, simply specify the current directory using '.'.

Accedi per commentare.


Guillaume
Guillaume il 23 Mag 2018
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 Commenti
priyanshu ashiya
priyanshu ashiya il 4 Lug 2021
Modificato: priyanshu ashiya il 4 Lug 2021
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 il 4 Lug 2021
@priyanshu ashiya What is the value of fid? If it's -1, the file was not able to be opened for some reason.

Accedi per commentare.

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Prodotti


Release

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by