Relative path gives wrong path
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hey, I have the problem that the relative path doesn't work for me on Linux with the function dir. Because the folder 'testfolder' is in the same folder where I saved the Matlab file I thought I could just say dir('testfolder'), but that does'nt work. Instead, when I just say dir, it gives me all the files and folders in /home/myname. I wrote this code on Windows and there it worked exactly like this, now want to run it on Linux. It works if I give the absolute path but that's not what I want... Is there any way I can change the folder that is used for relative paths? If it's not the folder where the file is saved, which one is chosen then? Thanks, Timo
EDIT: I found that it works when I say 'Change folder' sinstead of 'Add folder to path' at the very beginning when the respective window pops up. Still, I don't understand what it means to add the folder to the path. Could somebody please explain that?
0 Commenti
Risposte (1)
Stephen23
il 14 Feb 2018
Modificato: Stephen23
il 14 Feb 2018
"Still, I don't understand what it means to add the folder to the path"
Like many programming languages MATLAB has a path which is an explicit list of folder where it will look for code. It would not be efficient, useful, or probably even feasible for a program to look "everywhere" for code, thus an explicit list of folders is stored. This is called the "MATLAB path" or the "search path":
The current folder is implicitly always first on the MATLAB path, so the current folder will be searched first for code.
To use functions like dir simply define an absolute or relative path, following your OS and drives:
Rather than changing folders (which is slow and makes debugging harder) you should practice using absolute and relative paths (e.g. keep your data and code separately, and write your functions to accept the path of where the data is stored).
Vedere anche
Categorie
Scopri di più su File Operations in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!