フィルターのクリア

addpath from a different subfolder of the same parent directory

96 ビュー (過去 30 日間)
Luis J Gilarranz
Luis J Gilarranz 2019 年 10 月 23 日
コメント済み: Luis J Gilarranz 2019 年 10 月 23 日
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!

採用された回答

Guillaume
Guillaume 2019 年 10 月 23 日
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 件のコメント
Guillaume
Guillaume 2019 年 10 月 23 日
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.
Luis J Gilarranz
Luis J Gilarranz 2019 年 10 月 23 日
Perfect! that ../ does the trick. Thanks a lot.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSearch Path についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by