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

59 vues (au cours des 30 derniers jours)
Connor Sherod
Connor Sherod le 10 Juil 2018
Modifié(e) : Yash Trivedi le 10 Juil 2018
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');

Réponse acceptée

Yash Trivedi
Yash Trivedi le 10 Juil 2018
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 commentaires
Connor Sherod
Connor Sherod le 10 Juil 2018
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 le 10 Juil 2018
Modifié(e) : Yash Trivedi le 10 Juil 2018
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 -

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by