addpath from a different subfolder of the same parent directory

100 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Guillaume
Guillaume il 23 Ott 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 Commenti
Guillaume
Guillaume il 23 Ott 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Search Path in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by