Unable to load file using relative path
10 views (last 30 days)
Show older comments
I have folder structure as below
- code (which contains *.m files)
- data (which contains some data files)
I am trying to load resource from data files in one of the abc.m file. I have added both these folders in set path. But I am getting "No such file or directory error" while accessing data files. I am having below command in abc.m file
load('../data/xyz.mat','all_imagenames','mapping');
3 Comments
DMV
on 16 Oct 2018
I am also on Windows, and I have the same problem as the original poster. I've checked the path six times; I'm entering the correct relative path. I tried it with forward and backward slashes, just to be sure. Both paths (and all possible paths around, over, and under them) have been added and saved. I've also tried the data file's name with and without the '.mat' extension. I've tried all combinations of parentheses and single quotes around the path. What am I missing?!? Thanks
Jan
on 16 Oct 2018
@DMV: The original poster did not mention, what the current folder is. Adding folders "in set path" might mean the addpath command, which is a bad idea in general. Whenever you work with relative paths, the current folder is critical. If you set the current directory correctly, it should be trivial to work with absolute paths, which is a much better idea in general. Remember that all GUI and timer callbacks can change the current directory unexpectedly. If the current directory is e.g. "C:" (without trailing file separator) this is even more critical. Therefore it is strongly recommended to work with absolute paths. I guess then your problem will either vanish magically, or it becomes at least clear. Maybe you have a unicode character in the foldername? Or the path is on a network drive or dropbox folder?
I'm entering the correct relative path.
How did you check, that the path is "correct"? Which code causes the problem? What is the difference between the working check of correctness and the failing code?
I've tried all combinations of parentheses and single quotes
Please post the code and the error messages, because this is more useful that the description as text. I cannot imagine, where you have included parentheses or quote characters.
Answers (1)
Jan
on 16 Oct 2018
Edited: Jan
on 16 Oct 2018
It is a mess to add folders to Matlab's path only to import a file. There is no need to do so, but you might include unexpected M-files to Matlab's scope.
Working with relative paths is prone to errors. The callbacks of GUIs or timers can modify the current folder unexpectedly, so it is critical to rely on the value of the current directory. It is recommended to use absolute paths, because this removed the dependency to the current directory. See also: FileExchange: GetFullPath
The question is old and I do not expect to get feedback from the OP. I guess in the line
load('../data/xyz.mat','all_imagenames','mapping')
the assumption was wrong, what the current directory is. Maybe the OP assumed, that ".." starts from the directory, which contains the currently running function. This could be checked by (see the above link):
Folder = GetFullPath(fullfile(cd, '../data/xyz.mat'))
isfolder(Folder)
0 Comments
See Also
Categories
Find more on Filename Construction in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!