addpath from a different subfolder of the same parent directory

I have a matlab script in the following directory
PROJECT/JHON/script.m
And I need to acess filess that are in the following directory
PROJECT/MARIA/
If the "MARIA" folder were inside "JHON" it would be as easy as addpath('MARIA'), but I cannot change the folder structure, and I need to perform certain operations that require the path to be added to the search path.
Cheers!

 Accepted Answer

Data folder should never be added to the search path. It's dangerous (you may change what functions are in scope) and there's no need to anyway.
All matlab IO functions accept full paths of data file for import/export. It's much better to specify the full path of files. That way they can be anywhere you want, even on a network folder.
So, instead of something like:
%code that relies on the folder where mydatafile.txt resides to be on the path or be the current folder
data = csvread('mydatafile.txt'); %actual function used for importing is irrelevant
use
%code that works regardless of location of folder. Doesn't need to be on the path or be the current folder
folder = 'C:\somewhere\somefolder'; %location of mydatafile.txt. Could even be remote
data = csvread(fullfile(folder, 'mydatafile.txt'));

5 Comments

I've updated the question. No one is talking about data or csvreads. Since I don't intend to do any csvread, and the other folder contains a lot of stuff (and actually in my case, not a single csv). So now the question is more general and targeted to the actual problem. How to add to the search path a folder that is at the same hierarchical level in the directory as the folder where you have located your script.
Your question specifically says If the "data" folder ... so are you talking about data or not? My answer is not specific to csvread as I explicitly wrote.
Whatever the form of the data there shouldn't be any need to modify matlab path. You can always specify the full path of the data.
Note that this also applies to addpath. In your question example, you're using a relative path to your addpath call, so this relies on the current folder. And you're stuck because the current folder is inside the folder you want to add (and you don't know how to specify relative paths). If you specify a full path to addpath then it doesn't matter what the current folder is. Using full paths all the time makes your code more reliable since it doesn't rely on what the current folder is (which could be changed by some poorly written code you have no control over)
So, again, use full paths for whatever IO you're doing and you don't need to change the path at all. If it can't be helped:
addpath('/PROJECT/MARIA/') %or whatever the FULL PATH of that folder is.
Of course, if you add this folder to the path and some files in that folder have the same names as other files in other folders on the path, expect your code to behave unexpectly.
Yes, using full paths I can add whatever folder I want in whatever directory. But If then I want to share the code (as I have to) and be sure that it is going to work in another computer by just running without editing a single line, then absolute paths are not an option.
I could do cd.. then addpath('MARIA'), then do whatever I need to do there, and then cd back to my folder, but that's more cumbersome and requires 3 lines of code, versus some other elegant solution using just addpath and relative paths.
Cheers!
Assuming your current folder is PROJECT/JHON, then the relative path to PROJECT/MARIA/ is
addpath('../MARIA');
or you could still build a full path:
addpath(fullfile(pwd, '..', 'MARIA'));
I don't see how this solve your problem though since you still have to ensure that the current folder is indeed PROJECT/JHON/ on whichever computer is used.
Perfect! that ../ does the trick. Thanks a lot.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!