Relative path gives wrong path

5 次查看(过去 30 天)
Timo Sommer
Timo Sommer 2018-2-14
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?

回答(1 个)

Stephen23
Stephen23 2018-2-14
编辑:Stephen23 2018-2-14
"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).
  1 个评论
Timo Sommer
Timo Sommer 2018-2-17
Alright thank you. Though I'm not sure what you mean with using the relative path without cd. It didn't work for me to use the relative path to where the file is saved when I didn't change the folder. I have following situation: My file Testfile.m saved in the path /home/timo/Dokumente/test1. Besides the file there is another folder called test2. If I'm now simply running the command 'dir' in Testfile.m and clicking 'add to path' instead of 'changing the current folder' it shows me everyhting that is in /home, not in Test1 where it is saved. That is what I would understand as relative path, but it doesn't work.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by