addpath from a different subfolder of the same parent directory

84 views (last 30 days)
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

Guillaume
Guillaume on 23 Oct 2019
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
Guillaume
Guillaume on 23 Oct 2019
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Search Path 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!